Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float SliceBoxAndReturnVolume(int shipi, int boxi, t3dpoint<float> normal, float dst, t3dpoint<float> & COB)
- {
- ALOG("SLICING BOX AND RETURNING VOLUME");
- /*
- *
- * So this is how it goes:
- * First find all intersection points
- * Then calculate the center point of the intersections
- * this is our p that now we can calculate volume
- *
- */
- TPolygon<float> sliced_area;
- t3dpoint<float> A,B, output;
- for (int i = 0; i < 12; i++)
- {
- for (int j = 0; j < ship[ shipi ]->ByouancyBox[ boxi ].tris[i].Count; j++)
- {
- A = ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[i].V[j];
- int next = j + 1;
- if (next > ship[ shipi ]->ByouancyBox[ boxi ].tris[i].Count-1) next = 0;
- B = ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[i].V[ next ];
- if ( SegmentPlaneIntersection(normal, dst, A, B, output) )
- sliced_area.AddVertex( output );
- //int sajd = classify_point_plane( ship[ shipi ]->ByouancyBox[ boxi ].tris[i].V[j], normal, dst);
- //ALOG(" VERTEX SIDE: "+ IntToStr(sajd));
- }
- }
- //ALOG("SLICES: "+IntToStr(sliced_area.Count));
- //~~~~~~~~~~ Test if Area is fully submerged if is then return full volume else return 0
- if (sliced_area.Count == 0)
- if (classify_point_plane( ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[0].V[0], normal, dst) == isFront)
- {
- CAN_LOG = true;
- int shipside = classify_point_plane( ship[shipi]->pos, normal, dst);
- ALOG("ship aobve water return 0 btw calced side: "+IntToStr(shipside));
- ALOG("ship pos: "+POINT_TO_TEXT(ship[shipi]->pos));
- ALOG("box pos: "+POINT_TO_TEXT(ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[0].V[0]));
- // ALOG("POINT OF INTEREST: "+ POINT_TO_TEXT( ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[0].V[0]) );
- CAN_LOG = false;
- COB = ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].center;
- return 0.0f;
- }
- else
- {
- CAN_LOG = true;
- ALOG("Ship below water return box vol");
- // ALOG("RETURN ship vol: "+FloatToStr(ship[ shipi ]->ByouancyBox[ boxi ].Volume));
- CAN_LOG = false;
- COB = ship[shipi]->pos+ ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].center;
- return ship[ shipi ]->ByouancyBox[ boxi ].Volume;
- }
- //**************************if partially submerged
- t3dpoint<float> p = t3dpoint<float>(0.0, 0.0, 0.0);
- for (int i = 0; i < sliced_area.Count; i++)
- p = p + sliced_area.V[i];
- p = p / float(sliced_area.Count);
- //Almost done now to compute tetrahedrons volumes
- //in this case scenarion ship[ shipi ]->ByouancyBox[ boxi ].tris[i].Count is always 3
- float OutputVolume = 0.0;
- COB = t3dpoint<float>(0.0, 0.0, 0.0);
- int cobi = 0;
- for (int i = 0; i < 12; i++)
- {
- //see how many vertices are behind to know what type of cut we have
- int backfaces = 0;
- for (int j = 0; j < ship[ shipi ]->ByouancyBox[ boxi ].tris[i].Count; j++)
- {
- A = ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[i].V[j];
- if (classify_point_plane(A, normal, dst) == isBack) backfaces = backfaces + 1;
- }
- sliced_area.Count = 0;
- for (int j = 0; j < ship[ shipi ]->ByouancyBox[ boxi ].tris[i].Count; j++)
- {
- A = ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[i].V[j];
- if (classify_point_plane( A, normal, dst) == isBack)
- {
- sliced_area.AddVertex( A );
- COB = COB + A; cobi = cobi + 1;
- }
- int next = j + 1;
- if (next > ship[ shipi ]->ByouancyBox[ boxi ].tris[i].Count-1) next = 0;
- B = ship[shipi]->pos + ship[shipi]->WORLD_MAT * ship[ shipi ]->ByouancyBox[ boxi ].tris[i].V[ next ];
- if ( SegmentPlaneIntersection(normal, dst, A, B, output) )
- {
- sliced_area.AddVertex( output );
- COB = COB + output; cobi = cobi + 1;
- }
- }
- //CAN_LOG=true;
- //ALOG("BACK FACE TEST "+IntToStr(backfaces));
- //CAN_LOG=false;
- if (backfaces == 1)
- {
- OutputVolume = OutputVolume + TetrahedronVolume(
- sliced_area.V[0],
- sliced_area.V[1],
- sliced_area.V[2], p);
- } else //two triangle vertices under the water
- {
- OutputVolume = OutputVolume + TetrahedronVolume(
- sliced_area.V[0],
- sliced_area.V[1],
- sliced_area.V[2], p);
- OutputVolume = OutputVolume + TetrahedronVolume(
- sliced_area.V[2],
- sliced_area.V[3],
- sliced_area.V[0], p);
- }
- }
- COB = COB / float(cobi); //return center of byouancy
- CAN_LOG = true;
- ALOG(" ");
- ALOG(" RETURNING partially submerged volume: "+FloatToStr(OutputVolume));
- ALOG(" ");
- CAN_LOG = false;
- return OutputVolume;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement