RicardasSim

normal of triangle surface

Mar 21st, 2019
1,110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. //-std=c99 -Wall -Wextra -Wpedantic -Wshadow
  2. //linker -lm
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdbool.h>
  7. #include <math.h>
  8. #include <float.h>  //DBL_EPSILON
  9.  
  10. typedef struct{
  11.     double x;
  12.     double y;
  13.     double z;
  14. }Vector3;
  15.  
  16. typedef struct{
  17.     double x;
  18.     double y;
  19.     double z;
  20. }Point3;
  21.  
  22. void printVector(Vector3 v)
  23. {
  24.     printf("%f %f %f\n", v.x, v.y, v.z);
  25. }
  26.  
  27. bool isVectAlmZero(Vector3 v)
  28. {
  29.    
  30.     //if(fabs(v.x) > DBL_EPSILON || fabs(v.y) > DBL_EPSILON || fabs(v.z) > DBL_EPSILON) return false;
  31.     //return true;
  32.    
  33.     return !(fabs(v.x) > DBL_EPSILON || fabs(v.y) > DBL_EPSILON || fabs(v.z) > DBL_EPSILON);
  34.    
  35. }
  36.  
  37. Vector3 normalizeVec3(Vector3 v)
  38. {
  39.  
  40.     Vector3 retVec;
  41.    
  42.     double length = sqrt((v.x * v.x) + (v.y * v.y) + (v.z * v.z));
  43.    
  44.     retVec.x = v.x / length;    
  45.     retVec.y = v.y / length;
  46.     retVec.z = v.z / length;
  47.  
  48.     return retVec;
  49. }
  50.  
  51.  
  52. //normal of triangle surface
  53. Vector3 triangleSurfaceNormal(Point3 a, Point3 b, Point3 c)
  54. {    
  55.     Vector3 retVec;
  56.  
  57.     Vector3 edge1;
  58.     Vector3 edge2;
  59.    
  60.     edge1.x = b.x - a.x;
  61.     edge1.y = b.y - a.y;
  62.     edge1.z = b.z - a.z;
  63.  
  64.     edge2.x = c.x - a.x;
  65.     edge2.y = c.y - a.y;
  66.     edge2.z = c.z - a.z;
  67.    
  68.     //cross product
  69.     retVec.x = edge1.y * edge2.z - edge1.z * edge2.y;
  70.     retVec.y = edge1.z * edge2.x - edge1.x * edge2.z;
  71.     retVec.z = edge1.x * edge2.y - edge1.y * edge2.x;
  72.    
  73.     return retVec;
  74. }
  75.  
  76. int main (int argc, char **argv)
  77. {
  78.    
  79.     Point3 tstP1 = {0.0,0.0,0.0};
  80.     Point3 tstP2 = {0.5,0.0,0.0};
  81.     Point3 tstP3 = {0.5,0.5,0.0};
  82.  
  83.     Vector3 resultV;
  84.    
  85.     printf("CW: ");
  86.    
  87.     //CW, clockwise
  88.     resultV = triangleSurfaceNormal(tstP1,tstP2,tstP3);
  89.     printVector(resultV);
  90.    
  91.     if(isVectAlmZero(resultV))
  92.     {
  93.         printf("error: zero vector cannot be normalized.\n");
  94.         return 1;
  95.     }
  96.    
  97.     printf("CW (normalized): ");
  98.     printVector(normalizeVec3(resultV));
  99.    
  100.    
  101.     printf("CCW: ");
  102.    
  103.     //CCW, counter-clockwise
  104.     resultV = triangleSurfaceNormal(tstP1,tstP3,tstP2);
  105.     printVector(resultV);
  106.    
  107.     if(isVectAlmZero(resultV))
  108.     {
  109.         printf("error: zero vector cannot be normalized.\n");
  110.         return 1;
  111.     }
  112.    
  113.     printf("CCW (normalized): ");
  114.     printVector(normalizeVec3(resultV));
  115.    
  116.     printf("\n");
  117.  
  118.    
  119. return 0;
  120. }
  121.  
  122. /*
  123. output:
  124.  
  125. CW: 0.000000 0.000000 0.250000
  126. CW (normalized): 0.000000 0.000000 1.000000
  127. CCW: 0.000000 0.000000 -0.250000
  128. CCW (normalized): 0.000000 0.000000 -1.000000
  129.  
  130. */
Advertisement
Add Comment
Please, Sign In to add comment