Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.57 KB | None | 0 0
  1.  
  2. float SliceBoxAndReturnVolume(int shipi, int boxi, t3dpoint<float> normal, float dst, t3dpoint<float> & COB)
  3. {
  4.  
  5.     ALOG("SLICING BOX AND RETURNING VOLUME");
  6.  
  7. /*
  8.  *
  9.  *          So this is how it goes:
  10.  *          First find all intersection points
  11.  *          Then calculate the center point of the intersections
  12.  *          this is our p that now we can calculate volume
  13.  *
  14.  */
  15.  
  16.     TPolygon<float> sliced_area;
  17.     t3dpoint<float> A,B,    output;
  18.  
  19. for (int i = 0; i < 12; i++)
  20. {
  21.  
  22.     for (int j = 0; j < ship[ shipi ]->ByouancyBox[ boxi ].tris[i].Count; j++)
  23. {
  24.  
  25.     A = ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[i].V[j];
  26.     int next = j + 1;
  27.     if (next > ship[ shipi ]->ByouancyBox[ boxi ].tris[i].Count-1) next = 0;
  28.  
  29.     B = ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[i].V[ next ];
  30.  
  31.     if ( SegmentPlaneIntersection(normal, dst, A, B, output) )
  32.         sliced_area.AddVertex( output );
  33.  
  34.  
  35. //int sajd =    classify_point_plane( ship[ shipi ]->ByouancyBox[ boxi ].tris[i].V[j], normal, dst);
  36. //ALOG(" VERTEX SIDE: "+ IntToStr(sajd));
  37. }
  38. }
  39.  
  40. //ALOG("SLICES: "+IntToStr(sliced_area.Count));
  41. //~~~~~~~~~~ Test if Area is fully submerged if is then return full volume else return 0
  42.     if (sliced_area.Count == 0)
  43.     if (classify_point_plane( ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[0].V[0], normal, dst) == isFront)
  44.     {
  45.  
  46.         CAN_LOG = true;
  47.         int shipside = classify_point_plane( ship[shipi]->pos, normal, dst);
  48.         ALOG("ship aobve water return 0 btw calced side: "+IntToStr(shipside));
  49.         ALOG("ship pos: "+POINT_TO_TEXT(ship[shipi]->pos));
  50.         ALOG("box pos: "+POINT_TO_TEXT(ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[0].V[0]));
  51. //      ALOG("POINT OF INTEREST: "+ POINT_TO_TEXT( ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[0].V[0]) );
  52.         CAN_LOG = false;
  53.         COB = ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].center;
  54.         return 0.0f;
  55.     }
  56.     else
  57.     {
  58.         CAN_LOG = true;
  59.         ALOG("Ship below water return box vol");
  60. //      ALOG("RETURN ship vol: "+FloatToStr(ship[ shipi ]->ByouancyBox[ boxi ].Volume));
  61.         CAN_LOG = false;
  62.         COB = ship[shipi]->pos+ ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].center;
  63.         return ship[ shipi ]->ByouancyBox[ boxi ].Volume;
  64.     }
  65. //**************************if partially submerged
  66.  
  67. t3dpoint<float> p = t3dpoint<float>(0.0, 0.0, 0.0);
  68.     for (int i = 0; i < sliced_area.Count; i++)
  69.         p = p + sliced_area.V[i];
  70.     p = p / float(sliced_area.Count);
  71.  
  72. //Almost done now to compute tetrahedrons volumes
  73. //in this case scenarion  ship[ shipi ]->ByouancyBox[ boxi ].tris[i].Count is always 3
  74.  
  75.  
  76. float OutputVolume = 0.0;
  77. COB = t3dpoint<float>(0.0, 0.0, 0.0);
  78. int cobi = 0;
  79.     for (int i = 0; i < 12; i++)
  80.     {
  81.  
  82.  
  83. //see how many vertices are behind to know what type of cut we have
  84. int backfaces = 0;
  85.  
  86. for (int j = 0; j < ship[ shipi ]->ByouancyBox[ boxi ].tris[i].Count; j++)
  87. {
  88.     A = ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[i].V[j];
  89.     if (classify_point_plane(A, normal,  dst) == isBack)    backfaces = backfaces + 1;
  90. }
  91.  
  92. sliced_area.Count = 0;
  93.  
  94. for (int j = 0; j < ship[ shipi ]->ByouancyBox[ boxi ].tris[i].Count; j++)
  95. {
  96.  
  97.     A = ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[i].V[j];
  98.  
  99.     if (classify_point_plane( A, normal, dst) == isBack)
  100.     {
  101.         sliced_area.AddVertex( A );
  102.  
  103.         COB = COB + A; cobi = cobi + 1;
  104.     }
  105.  
  106.  
  107.             int next = j + 1;
  108.             if (next > ship[ shipi ]->ByouancyBox[ boxi ].tris[i].Count-1) next = 0;
  109.  
  110.             B = ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[i].V[ next ];
  111.  
  112.             if ( SegmentPlaneIntersection(normal, dst, A, B, output) )
  113.             {
  114.                 sliced_area.AddVertex( output );
  115.  
  116.                 COB = COB + output; cobi = cobi + 1;
  117.             }
  118.  
  119. }
  120. //CAN_LOG=true;
  121. //ALOG("BACK FACE TEST  "+IntToStr(backfaces));
  122. //CAN_LOG=false;
  123.  
  124.  
  125. if (backfaces == 1)
  126. {
  127.     OutputVolume = OutputVolume + TetrahedronVolume(
  128.             sliced_area.V[0],
  129.             sliced_area.V[1],
  130.             sliced_area.V[2], p);
  131. } else //two triangle vertices under the water
  132. {
  133.     OutputVolume = OutputVolume + TetrahedronVolume(
  134.             sliced_area.V[0],
  135.             sliced_area.V[1],
  136.             sliced_area.V[2], p);
  137.  
  138.     OutputVolume = OutputVolume + TetrahedronVolume(
  139.             sliced_area.V[2],
  140.             sliced_area.V[3],
  141.             sliced_area.V[0], p);
  142. }
  143.  
  144.  
  145.     }
  146.  
  147.  
  148.  
  149. COB = COB / float(cobi); //return center of byouancy
  150.  
  151.  
  152. CAN_LOG = true;
  153. ALOG(" ");
  154. ALOG(" RETURNING partially submerged volume: "+FloatToStr(OutputVolume));
  155. ALOG(" ");
  156. CAN_LOG = false;
  157.     return OutputVolume;
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement