1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* ---- includes ----------------------------------------------------------- */
18
19#include "b_BasicEm/Math.h"
20#include "b_TensorEm/IdCluster2D.h"
21
22/* ------------------------------------------------------------------------- */
23
24/* ========================================================================= */
25/*                                                                           */
26/* ---- \ghd{ auxiliary functions } ---------------------------------------- */
27/*                                                                           */
28/* ========================================================================= */
29
30/* ------------------------------------------------------------------------- */
31
32/* ========================================================================= */
33/*                                                                           */
34/* ---- \ghd{ constructor / destructor } ----------------------------------- */
35/*                                                                           */
36/* ========================================================================= */
37
38/* ------------------------------------------------------------------------- */
39
40void bts_IdCluster2D_init( struct bbs_Context* cpA,
41						   struct bts_IdCluster2D* ptrA )
42{
43	bts_Cluster2D_init( cpA, &ptrA->clusterE );
44	bbs_Int16Arr_init( cpA, &ptrA->idArrE );
45}
46
47/* ------------------------------------------------------------------------- */
48
49void bts_IdCluster2D_exit( struct bbs_Context* cpA,
50						   struct bts_IdCluster2D* ptrA )
51{
52	bts_Cluster2D_exit( cpA, &ptrA->clusterE );
53	bbs_Int16Arr_exit( cpA, &ptrA->idArrE );
54}
55
56/* ------------------------------------------------------------------------- */
57
58/* ========================================================================= */
59/*                                                                           */
60/* ---- \ghd{ operators } -------------------------------------------------- */
61/*                                                                           */
62/* ========================================================================= */
63
64/* ------------------------------------------------------------------------- */
65
66void bts_IdCluster2D_copy( struct bbs_Context* cpA,
67						   struct bts_IdCluster2D* ptrA,
68						   const struct bts_IdCluster2D* srcPtrA )
69{
70	bts_Cluster2D_copy( cpA, &ptrA->clusterE, &srcPtrA->clusterE );
71	bbs_Int16Arr_copy( cpA, &ptrA->idArrE, &srcPtrA->idArrE );
72}
73
74/* ------------------------------------------------------------------------- */
75
76flag bts_IdCluster2D_equal( struct bbs_Context* cpA,
77						    const struct bts_IdCluster2D* ptrA,
78							const struct bts_IdCluster2D* srcPtrA )
79{
80	if( !bts_Cluster2D_equal( cpA, &ptrA->clusterE, &srcPtrA->clusterE ) ) return FALSE;
81	if( !bbs_Int16Arr_equal( cpA, &ptrA->idArrE, &srcPtrA->idArrE ) ) return FALSE;
82	return TRUE;
83}
84
85/* ------------------------------------------------------------------------- */
86
87/* ========================================================================= */
88/*                                                                           */
89/* ---- \ghd{ query functions } -------------------------------------------- */
90/*                                                                           */
91/* ========================================================================= */
92
93/* ------------------------------------------------------------------------- */
94
95struct bts_Flt16Vec2D bts_IdCluster2D_center( struct bbs_Context* cpA,
96											  const struct bts_IdCluster2D* ptrA )
97{
98	return bts_Cluster2D_center( cpA, &ptrA->clusterE );
99}
100
101/* ------------------------------------------------------------------------- */
102
103struct bts_Int16Rect bts_IdCluster2D_boundingBox( struct bbs_Context* cpA,
104												  const struct bts_IdCluster2D* ptrA )
105{
106	return bts_Cluster2D_boundingBox( cpA, &ptrA->clusterE );
107}
108
109/* ------------------------------------------------------------------------- */
110
111/* ========================================================================= */
112/*                                                                           */
113/* ---- \ghd{ modify functions } ------------------------------------------- */
114/*                                                                           */
115/* ========================================================================= */
116
117/* ------------------------------------------------------------------------- */
118
119void bts_IdCluster2D_create( struct bbs_Context* cpA,
120							 struct bts_IdCluster2D* ptrA,
121							 uint32 sizeA,
122						     struct bbs_MemSeg* mspA )
123{
124	if( bbs_Context_error( cpA ) ) return;
125	bts_Cluster2D_create( cpA, &ptrA->clusterE, sizeA, mspA );
126	bbs_Int16Arr_create( cpA, &ptrA->idArrE, sizeA, mspA );
127}
128
129/* ------------------------------------------------------------------------- */
130
131void bts_IdCluster2D_size( struct bbs_Context* cpA,
132						   struct bts_IdCluster2D* ptrA,
133						   uint32 sizeA )
134{
135	bts_Cluster2D_size( cpA, &ptrA->clusterE, sizeA );
136	bbs_Int16Arr_size( cpA, &ptrA->idArrE, sizeA );
137}
138
139/* ------------------------------------------------------------------------- */
140
141void bts_IdCluster2D_transform( struct bbs_Context* cpA,
142							    struct bts_IdCluster2D* ptrA,
143								struct bts_Flt16Alt2D altA )
144{
145	bts_Cluster2D_transform( cpA, &ptrA->clusterE, altA );
146}
147
148/* ------------------------------------------------------------------------- */
149
150void bts_IdCluster2D_copyTransform( struct bbs_Context* cpA,
151								    struct bts_IdCluster2D* ptrA,
152									const struct bts_IdCluster2D* srcPtrA,
153									struct bts_Flt16Alt2D altA,
154									uint32 dstBbpA )
155{
156	bts_Cluster2D_copyTransform( cpA, &ptrA->clusterE, &srcPtrA->clusterE, altA, dstBbpA );
157	bbs_Int16Arr_copy( cpA, &ptrA->idArrE, &srcPtrA->idArrE );
158}
159
160/* ------------------------------------------------------------------------- */
161
162/* ========================================================================= */
163/*                                                                           */
164/* ---- \ghd{ I/O } -------------------------------------------------------- */
165/*                                                                           */
166/* ========================================================================= */
167
168/* ------------------------------------------------------------------------- */
169
170uint32 bts_IdCluster2D_memSize( struct bbs_Context* cpA,
171							    const struct bts_IdCluster2D *ptrA )
172{
173	return  bbs_SIZEOF16( uint32 )
174		  + bbs_SIZEOF16( uint32 ) /* version */
175		  +	bts_Cluster2D_memSize( cpA, &ptrA->clusterE )
176		  + bbs_Int16Arr_memSize( cpA, &ptrA->idArrE );
177}
178
179/* ------------------------------------------------------------------------- */
180
181uint32 bts_IdCluster2D_memWrite( struct bbs_Context* cpA,
182								 const struct bts_IdCluster2D* ptrA,
183								 uint16* memPtrA )
184{
185	uint32 memSizeL = bts_IdCluster2D_memSize( cpA, ptrA );
186	memPtrA += bbs_memWrite32( &memSizeL, memPtrA );
187	memPtrA += bbs_memWriteUInt32( bts_ID_CLUSTER2D_VERSION, memPtrA );
188	memPtrA += bts_Cluster2D_memWrite( cpA, &ptrA->clusterE, memPtrA );
189	memPtrA += bbs_Int16Arr_memWrite( cpA, &ptrA->idArrE, memPtrA );
190	return memSizeL;
191}
192
193/* ------------------------------------------------------------------------- */
194
195uint32 bts_IdCluster2D_memRead( struct bbs_Context* cpA,
196							    struct bts_IdCluster2D* ptrA,
197							    const uint16* memPtrA,
198						        struct bbs_MemSeg* mspA )
199{
200	uint32 memSizeL;
201	uint32 versionL;
202	if( bbs_Context_error( cpA ) ) return 0;
203	memPtrA += bbs_memRead32( &memSizeL, memPtrA );
204	memPtrA += bbs_memReadVersion32( cpA, &versionL, bts_ID_CLUSTER2D_VERSION, memPtrA );
205	memPtrA += bts_Cluster2D_memRead( cpA, &ptrA->clusterE, memPtrA, mspA );
206	memPtrA += bbs_Int16Arr_memRead( cpA, &ptrA->idArrE, memPtrA, mspA );
207	if( memSizeL != bts_IdCluster2D_memSize( cpA, ptrA ) )
208	{
209		bbs_ERR0( bbs_ERR_CORRUPT_DATA, "uint32 bts_IdCluster2D_memRead( const struct bts_IdCluster2D* ptrA, const void* memPtrA ):\n"
210                   "size mismatch" );
211		return 0;
212	}
213	return memSizeL;
214}
215
216/* ------------------------------------------------------------------------- */
217
218/* ========================================================================= */
219/*                                                                           */
220/* ---- \ghd{ exec functions } --------------------------------------------- */
221/*                                                                           */
222/* ========================================================================= */
223
224/* ------------------------------------------------------------------------- */
225
226void bts_IdCluster2D_convertToEqivalentClusters( struct bbs_Context* cpA,
227												 const struct bts_IdCluster2D* inCluster1PtrA,
228												 const struct bts_IdCluster2D* inCluster2PtrA,
229												 struct bts_Cluster2D* outCluster1PtrA,
230												 struct bts_Cluster2D* outCluster2PtrA )
231{
232	uint32 iL, jL;
233	uint32 countL = 0;
234
235	uint32 size1L = inCluster1PtrA->clusterE.sizeE;
236	uint32 size2L = inCluster2PtrA->clusterE.sizeE;
237
238	const int16* idArr1L = inCluster1PtrA->idArrE.arrPtrE;
239	const int16* idArr2L = inCluster2PtrA->idArrE.arrPtrE;
240
241	const struct bts_Int16Vec2D* srcVecArr1E = inCluster1PtrA->clusterE.vecArrE;
242	const struct bts_Int16Vec2D* srcVecArr2E = inCluster2PtrA->clusterE.vecArrE;
243
244	struct bts_Int16Vec2D* dstVecArr1E = outCluster1PtrA->vecArrE;
245	struct bts_Int16Vec2D* dstVecArr2E = outCluster2PtrA->vecArrE;
246
247	uint32 maxOutSizeL = bbs_min( outCluster1PtrA->allocatedSizeE, outCluster2PtrA->allocatedSizeE );
248	bts_Cluster2D_size( cpA, outCluster1PtrA, maxOutSizeL );
249	bts_Cluster2D_size( cpA, outCluster2PtrA, maxOutSizeL );
250
251	for( iL = 0; iL < size1L; iL++ )
252	{
253		int32 idL = idArr1L[ iL ];
254		if( idL >= 0 )
255		{
256			for( jL = 0; jL < size2L; jL++ )
257			{
258				if( idL == idArr2L[ jL ] ) break;
259			}
260
261			if( jL < size2L )
262			{
263				if( countL == maxOutSizeL )
264				{
265					bbs_ERROR0( "void bts_IdCluster2D_convertToEqivalentClusters( .... ):\n"
266						       "Destination clusters are insufficiently allocated" );
267					return;
268				}
269
270				dstVecArr1E[ countL ] = srcVecArr1E[ iL ];
271				dstVecArr2E[ countL ] = srcVecArr2E[ jL ];
272				countL++;
273			}
274		}
275	}
276
277	bts_Cluster2D_size( cpA, outCluster1PtrA, countL );
278	bts_Cluster2D_size( cpA, outCluster2PtrA, countL );
279
280	outCluster1PtrA->bbpE = inCluster1PtrA->clusterE.bbpE;
281	outCluster2PtrA->bbpE = inCluster2PtrA->clusterE.bbpE;
282}
283
284/* ------------------------------------------------------------------------- */
285
286struct bts_Flt16Alt2D bts_IdCluster2D_alt( struct bbs_Context* cpA,
287										   const struct bts_IdCluster2D* srcPtrA,
288										   struct bts_IdCluster2D* dstPtrA,
289										   enum bts_AltType altTypeA,
290										   struct bts_Cluster2D* tmpPtr1A,  /* temporary cluster 1 */
291										   struct bts_Cluster2D* tmpPtr2A ) /* temporary cluster 2 */
292{
293	bts_IdCluster2D_convertToEqivalentClusters( cpA, srcPtrA, dstPtrA, tmpPtr1A, tmpPtr2A );
294	return bts_Cluster2D_alt( cpA, tmpPtr1A, tmpPtr2A, altTypeA );
295}
296
297/* ------------------------------------------------------------------------- */
298
299/* ========================================================================= */
300
301
302