FreeWRL/FreeX3D  3.0.0
NormalCalcs.c
1 /*
2 
3 
4 ???
5 
6 */
7 
8 
9 /****************************************************************************
10  This file is part of the FreeWRL/FreeX3D Distribution.
11 
12  Copyright 2009 CRC Canada. (http://www.crc.gc.ca)
13 
14  FreeWRL/FreeX3D is free software: you can redistribute it and/or modify
15  it under the terms of the GNU Lesser Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  FreeWRL/FreeX3D is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  GNU General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with FreeWRL/FreeX3D. If not, see <http://www.gnu.org/licenses/>.
26 ****************************************************************************/
27 
28 
29 
30 #include <config.h>
31 #include <system.h>
32 #include <display.h>
33 #include <internal.h>
34 
35 #include <libFreeWRL.h>
36 
37 #include "../vrml_parser/Structs.h"
38 #include "../main/headers.h"
39 
40 #include "LinearAlgebra.h"
41 
42 
43 void fwnorprint (float *norm) {
44  printf ("normals %f %f %f\n",norm[0],norm[1],norm[2]);
45 }
46 
47 void normalize_ifs_face (float *point_normal,
48  struct point_XYZ *facenormals,
49  int *pointfaces,
50  int mypoint,
51  int curpoly,
52  float creaseAngle) {
53 
54  /* IndexedFaceSet (and possibly sometime, others)
55  normal generator
56 
57  Passed in:
58  point_normal - where to put the calculated normal
59  facenormals - normals of each face of a polygon
60  pointfaces - each point - which face(s) is it part of
61  mypoint - which point are we looking at
62  curpoly - which poly (face) we are working on
63  creaseAngle - creaseAngle of polygon
64  */
65  int tmp_a;
66  int tmp_b;
67  float zz;
68  struct point_XYZ temp;
69  bool foundInOtherFaces = false;
70 
71  point_normal[0] = 0.0f; point_normal[1] = 0.0f; point_normal[2] = 0.0f;
72 
73  //printf ("\nstart normalize_ifs_face\n");
74  //printf ("my normal is %f %f %f\n", facenormals[curpoly].x,facenormals[curpoly].y,facenormals[curpoly].z);
75 
76  /* short cut for a point in only 1 face */
77  if (pointfaces[mypoint*POINT_FACES] == 1) {
78  point_normal[0]=(float) facenormals[curpoly].x;
79  point_normal[1]=(float) facenormals[curpoly].y;
80  point_normal[2]=(float) facenormals[curpoly].z;
81  //printf ("normalize_ifs_face: quick return normalized vector is %f %f %f\n",point_normal[0], point_normal[1], point_normal[2]);
82  return;
83  }
84 
85  /* ok, calculate normal */
86  for (tmp_b=0; tmp_b<pointfaces[mypoint*POINT_FACES]; tmp_b++) {
87  tmp_a = pointfaces[mypoint*POINT_FACES+tmp_b+1];
88  //printf ("comparing myface %d to %d\n",curpoly,tmp_a);
89 
90  if (curpoly == tmp_a) {
91  zz = 0.0f;
92  } else {
93  zz = calc_angle_between_two_vectors(facenormals[curpoly],facenormals[tmp_a] );
94  }
95  //printf ("angle between faces is %f, creaseAngle is %f\n",zz,creaseAngle);
96 
97 
98  if (zz <= creaseAngle) {
99  //printf ("count this one in; adding %f %f %f\n",facenormals[tmp_a].x,facenormals[tmp_a].y,facenormals[tmp_a].z);
100  foundInOtherFaces = true;
101  point_normal[0] += (float) facenormals[tmp_a].x;
102  point_normal[1] += (float) facenormals[tmp_a].y;
103  point_normal[2] += (float) facenormals[tmp_a].z;
104  }
105  }
106 
107  // do we have to average this one, or should we just return our original normal?
108  if (foundInOtherFaces) {
109  temp.x = point_normal[0]; temp.y=point_normal[1]; temp.z=point_normal[2];
110  normalize_vector(&temp);
111  point_normal[0]=(float) temp.x; point_normal[1]=(float) temp.y; point_normal[2]=(float) temp.z;
112  } else {
113  //printf ("false alarm - just copy original over");
114  point_normal[0]=(float) facenormals[curpoly].x;
115  point_normal[1]=(float) facenormals[curpoly].y;
116  point_normal[2]=(float) facenormals[curpoly].z;
117  }
118 
119  //printf ("normalize_ifs_face: normalized vector is %f %f %f\n",point_normal[0], point_normal[1], point_normal[2]);
120 }