Hitmap 1.3
 All Data Structures Namespaces Files Functions Variables Typedefs Friends Macros Groups Pages
hit_shape.c
Go to the documentation of this file.
1 
13 /*
14  * <license>
15  *
16  * Hitmap v1.2
17  *
18  * This software is provided to enhance knowledge and encourage progress in the scientific
19  * community. It should be used only for research and educational purposes. Any reproduction
20  * or use for commercial purpose, public redistribution, in source or binary forms, with or
21  * without modifications, is NOT ALLOWED without the previous authorization of the copyright
22  * holder. The origin of this software must not be misrepresented; you must not claim that you
23  * wrote the original software. If you use this software for any purpose (e.g. publication),
24  * a reference to the software package and the authors must be included.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND ANY
27  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
29  * THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  * Copyright (c) 2007-2015, Trasgo Group, Universidad de Valladolid.
37  * All rights reserved.
38  *
39  * More information on http://trasgo.infor.uva.es/
40  *
41  * </license>
42 */
43 
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdarg.h>
47 #include <stdlib.h>
48 #include <limits.h>
49 #include <hit_allocP.h>
50 #include <hit_sshape.h>
51 #include <hit_cshape.h>
52 #include <hit_bshape.h>
53 #include <unistd.h>
54 #include <hit_funcop.h>
55 #include <hit_error.h>
56 
57 
58 
59 /* 2. Hit SHAPE NULL CONSTANTs */
60 HitShape HIT_SHAPE_NULL = HIT_SHAPE_NULL_STATIC;
61 // @author arturo
62 HitShape HIT_SIG_SHAPE_NULL = HIT_SIG_SHAPE_NULL_STATIC;
63 // @author arturo
64 #ifdef __cplusplus
66  HIT_CSR_SHAPE_NULL.info.csr = HIT_CSR_SHAPE_INTERNAL_NULL_STATIC;
68  HIT_BITMAP_SHAPE_NULL.info.csr = HIT_BITMAP_SHAPE_INTERNAL_NULL_STATIC;
69 #else
70 HitShape HIT_CSR_SHAPE_NULL = HIT_CSR_SHAPE_NULL_STATIC;
71 HitShape HIT_BITMAP_SHAPE_NULL = HIT_BITMAP_SHAPE_NULL_STATIC;
72 #endif
73 
74 /* 3. Hit SHAPE WHOLE CONSTANT */
75 HitShape HIT_SHAPE_WHOLE = HIT_SHAPE_WHOLE_STATIC;
76 // @author arturo
77 HitSigShape HIT_SIG_SHAPE_WHOLE = HIT_SIG_SHAPE_WHOLE_STATIC;
78 
79 /* 4. Hit Name List constant */
80 HitNameList HIT_NAMELIST_NULL = HIT_NAMELIST_NULL_STATIC;
81 
82 
83 /* 6. HIT SHAPE FREE FUNCTION */
85 
86  switch (hit_shapeType(shape)){
87  // Do nothing if it is a Signature Shape.
88  case HIT_SIG_SHAPE:
89  return;
90  // CSR Shape.
91  case HIT_CSR_SHAPE:
92  hit_cShapeFree(shape);
93  return;
94  // Bitmap Shape.
95  case HIT_BITMAP_SHAPE:
96  hit_bShapeFree(shape);
97  return;
98  default:
99  hit_errInternal(__func__, "Unsupported shape type", "", __FILE__, __LINE__);
100  break;
101  }
102 
103 
104 
105 }
106 
107 
108 /* 7. Hit SHAPE: COMPARISON OPERATOR */
110 
111  // Only works for Signature Shape.
112  if( hit_shapeType(sh1) == HIT_CSR_SHAPE ||
113  hit_shapeType(sh2) == HIT_CSR_SHAPE ) return 0;
114 
115  if (hit_shapeDims(sh1) != hit_shapeDims(sh2) ) return 0;
116  int i;
117  for (i=0; i<hit_shapeDims(sh1); i++)
118  if ( ! hit_sigCmp( hit_shapeSig(sh1,i), hit_shapeSig(sh2,i) ) ) return 0;
119  return 1;
120 }
121 
122 /* 8. Hit INTERSECT SHAPES */
124 
125  /* 0. ONLY FOR SIGNATURE SHAPE */
126  if( hit_shapeType(sh1) != HIT_SIG_SHAPE ||
127  hit_shapeType(sh2) != HIT_SIG_SHAPE ) return HIT_SHAPE_NULL;
128 
129  HitShape res = HIT_SHAPE_NULL;
130 
131  /* 1. DIFFERENT DIMENSIONS, RETURN EMPTY SHAPE */
132  if (hit_shapeDims(sh1) != hit_shapeDims(sh2)) return res;
133 
134  /* 2. FOR EACH DIMENSION IN BOTH SHAPES. OBSOLETE: DIMENSIONS ARE EQUAL */
135  int minDims = ( hit_shapeDims(sh1) < hit_shapeDims(sh2) ) ? hit_shapeDims(sh1) : hit_shapeDims(sh2);
136 
137  int i;
138  int null = 0; // FLAG: CHECK IF ALL DIMENSIONS HAVE NON-EMPTY INTERSECTION
139  for (i=0; i<minDims; i++) {
140  /* 2.1. COMPUTE INTERSECTION */
141  hit_shapeSig(res,i) = hit_sigIntersect( hit_shapeSig(sh1,i), hit_shapeSig(sh2,i) );
142 
143  /* 2.2. CHECK IF THERE IS A NON-NULL SIGNATURE */
144  if ( hit_sigCmp( hit_shapeSig(res,i), HIT_SIG_NULL ) ) null = 1;
145  }
146 
147  /* 3. IF NO DIMENSIONAL INTERSECTION, RETURN NULL SHAPE */
148  if ( null ) return HIT_SHAPE_NULL;
149 
150  /* 4. RETURN NON-EMPTY INTERSECTION */
151  hit_shapeDimsSet( res, minDims );
152  return res;
153 }
154 
155 
156 /* 8.2. Hit SUBSELECTION OF SHAPES */
158 
159  /* 0. ONLY FOR SIGNATURE SHAPE */
160  if( hit_shapeType(sh1) != HIT_SIG_SHAPE ||
161  hit_shapeType(sh2) != HIT_SIG_SHAPE ) return HIT_SHAPE_NULL;
162 
163  /* 1. DIFFERENT DIMENSIONS, RETURN EMPTY SHAPE */
164  if (hit_shapeDims(sh1) != hit_shapeDims(sh2)) return HIT_SHAPE_NULL;
165 
166  /* 2. FOR EACH DIMENSION IN BOTH SHAPES: OBSOLETE IF DIMENSIONS ARE EQUAL */
167  HitShape res = HIT_SHAPE_NULL;
168  int minDims = ( hit_shapeDims(sh1) < hit_shapeDims(sh2) ) ? hit_shapeDims(sh1) : hit_shapeDims(sh2);
169  int i;
170  for (i=0; i<minDims; i++) {
171  /* 2.1. COMPUTE SELECTION */
172  hit_shapeSig(res,i) = hit_sigBlend( hit_shapeSig(sh1,i), hit_shapeSig(sh2,i) );
173  }
174 
175  /* 3. RETURN */
176  hit_shapeDimsSet( res, minDims );
177  return res;
178 }
179 
180 /* 8.3. Hit TRANSFORM COORDINATES */
182 
183  /* 0. ONLY FOR SIGNATURE SHAPE */
184  if( hit_shapeType(sh1) != HIT_SIG_SHAPE ||
185  hit_shapeType(sh2) != HIT_SIG_SHAPE ) return HIT_SHAPE_NULL;
186 
187  /* 1. DIFFERENT DIMENSIONS, RETURN EMPTY SHAPE */
188  if (hit_shapeDims(sh1) != hit_shapeDims(sh2)) return HIT_SHAPE_NULL;
189 
190  /* 2. FOR EACH DIMENSION IN BOTH SHAPES: OBSOLETE IF DIMENSIONS ARE EQUAL */
191  HitShape res = HIT_SHAPE_NULL;
192  int minDims = ( hit_shapeDims(sh1) < hit_shapeDims(sh2) ) ? hit_shapeDims(sh1) : hit_shapeDims(sh2);
193  int i;
194  for (i=0; i<minDims; i++) {
195  /* 2.1. COMPUTE TRANSFORMATION */
196  hit_shapeSig(res,i).begin =
197  hit_sigTileToArray( hit_shapeSig(sh1,i), hit_shapeSig(sh2,i).begin );
198  hit_shapeSig(res,i).end =
199  hit_sigTileToArray( hit_shapeSig(sh1,i), hit_shapeSig(sh2,i).end );
200  hit_shapeSig(res,i).stride = hit_shapeSig(sh1,i).stride * hit_shapeSig(sh2,i).stride;
201  }
202 
203  /* 3. RETURN */
204  hit_shapeDimsSet( res, minDims );
205  return res;
206 }
207 
208 
209 /* 9.1 Hit SHAPE EXPAND MANY DIMS IN EQUAL OFFSET */
211 
212  // Only works for Signature Shape.
213  if( hit_shapeType(shape) == HIT_CSR_SHAPE ) return HIT_SHAPE_NULL;
214 
215  HitShape res = shape;
216 
217  int dim;
218  for(dim=0;dim<dims;dim++){
219  hit_shapeSig(res,dim).begin-=offset;
220  hit_shapeSig(res,dim).end+=offset;
221  }
222  return res;
223 }
224 
225 /* 9.1 Hit SHAPE EXPAND A DIM IN A GIVEN DIRECTION */
226 HitShape hit_shapeDimExpand(HitShape shape,int dim, int position, int offset ){
227 
228  // Only works for Signature Shape.
229  if( hit_shapeType(shape) == HIT_CSR_SHAPE ) return HIT_SHAPE_NULL;
230 
231  HitShape res = shape;
232 
233  if ( position == HIT_SHAPE_BEGIN ) hit_shapeSig(res,dim).begin += offset;
234  else hit_shapeSig(res,dim).end += offset;
235 
236  return res;
237 }
238 
239 
240 /* 9.2 Hit SHAPE GET BORDER */
241 HitShape hit_shapeBorder(HitShape shape, int dim, int position, int offset){
242 
243  // Only works for Signature Shape.
244  if( hit_shapeType(shape) == HIT_CSR_SHAPE ) return HIT_SHAPE_NULL;
245 
246  HitShape res = shape;
247 
248  if(position == HIT_SHAPE_BEGIN){
249  hit_shapeSig(res,dim).begin -= offset;
250  hit_shapeSig(res,dim).end = hit_shapeSig(res,dim).begin;
251  } else {
252  hit_shapeSig(res,dim).end += offset;
253  hit_shapeSig(res,dim).begin = hit_shapeSig(res,dim).end;
254  }
255  return res;
256 }
257 
258 
259 
260 
261 /* 12 */
262 int hit_nameListName2Index(HitNameList list, int name){
263 
264  // A. There is a inv array.
265  if( list.flagNames == HIT_SHAPE_NAMES_ARRAY ){
266  if(name >= list.nInvNames) return -1;
267  return list.invNames[name];
268  }
269 
270  // B. There is no inv array, but the names are ordered from 0 to N.
271  if( list.flagNames == HIT_SHAPE_NAMES_ORDERED ){
272  if( name < 0 || name >= list.nNames ) return -1;
273  return name;
274  }
275 
276  // C. There is no inv array, we have to loop through all the vertices.
277  if( list.flagNames == HIT_SHAPE_NAMES_NOARRAY ){
278  // Search the local name.
279  int i;
280  for(i=0; i<list.nNames; i++){
281  if(list.names[i] == name) return i;
282  }
283  return -1;
284  }
285 
286  return -1;
287 }
288 
289 
290 
291 void hit_nameListFree(HitNameList list){
292 
293  hit_free(list.names);
294 
295  if(list.flagNames == HIT_SHAPE_NAMES_ARRAY){
296  hit_free(list.invNames);
297  }
298 }
299 
300 
301 void hit_nameListCreate(HitNameList * list, int nelems){
302 
303  *list = HIT_NAMELIST_NULL;
304 
305  list->nNames = nelems;
306  // @arturo Ago 2015: New allocP interface
307  // hit_malloc(list->names,(size_t) nelems * sizeof(int),int*);
308  hit_malloc(list->names, int, nelems );
309 
310  // Fill the global names array
311  int i;
312  for(i=0;i<nelems;i++){
313  list->names[i] = i;
314  }
315  list->flagNames = HIT_SHAPE_NAMES_ORDERED;
316 }
317 
318 void hit_nameListClone(HitNameList * dst, HitNameList * src){
319 
320  *dst = HIT_NAMELIST_NULL;
321 
322  dst->nNames = src->nNames;
323  // @arturo Ago 2015: New allocP interface
324  // hit_malloc(dst->names,(size_t) dst->nNames * sizeof(int),int*);
325  hit_malloc(dst->names, int, dst->nNames );
326 
327  memcpy(dst->names, src->names, (size_t) dst->nNames * sizeof(int) );
328 
329  dst->flagNames = HIT_SHAPE_NAMES_ORDERED;
330 
331 
332 }
333 
334 
335 
336 
337 void hit_nameListAdd(HitNameList * list, int x){
338 
339  if(list->nNames == 0){
340 
341  list->nNames = 1;
342  // @arturo Ago 2015: New allocP interface
343  // hit_malloc(list->names,sizeof(int),int*);
344  hit_malloc(list->names, int, 1);
345  list->names[0] = x;
346 
347  } else {
348 
349  list->nNames++;
350  // @arturo Ago 2015: New allocP interface
351  // hit_realloc(list->names,(size_t) list->nNames * sizeof(int),int*);
352  hit_realloc(list->names, int, list->nNames );
353  list->names[list->nNames-1] = x;
354 
355  }
356 
357  list->flagNames = HIT_SHAPE_NAMES_NOARRAY;
358 }
359 
360 
361 void hit_nameListCreateInvNames(HitNameList * list){
362 
363  // 1. Set the flag.
364  list->flagNames = HIT_SHAPE_NAMES_ARRAY;
365 
366  // 2. Get the maximum name that will determine the size of the array.
367  int name_max = 0;
368  int i;
369  for(i=0; i<list->nNames; i++){
370  name_max = hit_max(name_max,list->names[i]);
371  }
372  name_max++;
373  list->nInvNames = name_max;
374 
375  // 3. Allocate the array.
376  // @arturo Ago 2015: New allocP interface
377  // hit_malloc(list->invNames, (size_t) (name_max+1) * sizeof(int),int*);
378  hit_malloc(list->invNames, int, name_max+1 );
379 
380  // 4. Set the array values.
381  for(i=0; i<name_max; i++){
382  list->invNames[i] = -1;
383  }
384  for(i=0; i<list->nNames; i++){
385  list->invNames[ list->names[i] ] = i;
386  }
387 }
388 
389 /* PRINT A SHAPE*/
390 void
392 {
393 
394  int i;
395  printf ("[%d:%d:%d", hit_shapeSig (sh, 0).begin, hit_shapeSig (sh, 0).end,
396  hit_shapeSig (sh, 0).stride);
397  for (i = 1; i < hit_shapeDims (sh); i++)
398  printf (",%d:%d:%d", hit_shapeSig (sh, i).begin, hit_shapeSig (sh, i).end,
399  hit_shapeSig (sh, i).stride);
400  printf ("] \t cards: [%d", hit_sigCard (hit_shapeSig (sh, 0)));
401  for (i = 1; i < hit_shapeDims (sh); i++)
402  printf (",%d", hit_sigCard (hit_shapeSig (sh, i)));
403  printf ("]\n");
404 }
void hit_nameListCreateInvNames(HitNameList *list)
Definition: hit_shape.c:361
HitShape hit_shapeBorder(HitShape shape, int dim, int position, int offset)
Definition: hit_shape.c:241
#define hit_free(ptr)
Definition: hit_allocP.h:152
void hit_nameListClone(HitNameList *dst, HitNameList *src)
Definition: hit_shape.c:318
HitShape hit_shapeExpand(HitShape shape, int dims, int offset)
Definition: hit_shape.c:210
HitShape hit_shapeIntersect(HitShape sh1, HitShape sh2)
Definition: hit_shape.c:123
#define hit_shapeDimsSet(shape, value)
Definition: hit_sshape.h:387
void hit_nameListAdd(HitNameList *list, int x)
Definition: hit_shape.c:337
int hit_nameListName2Index(HitNameList list, int name)
Definition: hit_shape.c:262
HitShape HIT_SIG_SHAPE_NULL
Definition: hit_shape.c:62
int hit_shapeCmp(HitShape sh1, HitShape sh2)
Definition: hit_shape.c:109
HitShape HIT_SHAPE_WHOLE
Definition: hit_shape.c:75
#define hit_sigCard(sig)
Definition: hit_sig.h:162
HitShape hit_shapeTileToArray(HitShape sh1, HitShape sh2)
Definition: hit_shape.c:181
void hit_nameListFree(HitNameList list)
Definition: hit_shape.c:291
HitSig hit_sigIntersect(HitSig s1, HitSig s2)
Definition: hit_sig.c:86
#define HIT_SIG_SHAPE
Definition: hit_shape.h:171
#define HIT_BITMAP_SHAPE
Definition: hit_shape.h:183
int dims[2]
Definition: SWpar_ref.c:187
void hit_nameListCreate(HitNameList *list, int nelems)
Definition: hit_shape.c:301
void hit_bShapeFree(HitShape shape)
Definition: hit_bshape.c:95
HitSig HIT_SIG_NULL
Definition: hit_sig.c:48
#define hit_shapeDims(shape)
Definition: hit_sshape.h:364
#define x
Hitmap functions to allocate memory.
#define hit_malloc(ptr, type, nmemb)
Definition: hit_allocP.h:93
#define HIT_CSR_SHAPE
Definition: hit_shape.h:177
HitShape HIT_SHAPE_NULL
Definition: hit_shape.c:60
HitShape shape
HitShape hit_shapeSubset(HitShape sh1, HitShape sh2)
Definition: hit_shape.c:157
HitShape HIT_CSR_SHAPE_NULL
Definition: hit_shape.c:70
void hit_cShapeFree(HitShape shape)
Definition: hit_cshape.c:99
HitNameList HIT_NAMELIST_NULL
Definition: hit_shape.c:80
#define hit_shapeType(s)
Definition: hit_shape.h:266
HitShape HIT_BITMAP_SHAPE_NULL
Definition: hit_shape.c:71
#define hit_realloc(ptr, type, nmemb)
Definition: hit_allocP.h:134
HitShape hit_shapeDimExpand(HitShape shape, int dim, int position, int offset)
Definition: hit_shape.c:226
#define hit_max(a, b)
Definition: hit_funcop.h:59
#define hit_shapeSig(shape, dim)
Definition: hit_sshape.h:400
void hit_shapeFree(HitShape s)
Definition: hit_shape.c:84
#define hit_errInternal(routine, text, extraParam, file, numLine)
Definition: hit_error.h:63
#define hit_sigTileToArray(sig, ind)
Definition: hit_sig.h:198
#define hit_sigCmp(s1, s2)
Definition: hit_sig.h:174
void dumpShape(HitShape sh)
Definition: hit_shape.c:391
HitSigShape HIT_SIG_SHAPE_WHOLE
Definition: hit_shape.c:77
#define HIT_SHAPE_BEGIN
Definition: hit_sshape.h:515