Skip to content

Commit 8af625d

Browse files
committed
Document collisionMesh.h
1 parent b75f8bf commit 8af625d

File tree

1 file changed

+192
-5
lines changed

1 file changed

+192
-5
lines changed

include/Collision/collisionMesh.h

+192-5
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,27 @@ namespace Hop::System::Physics
2020
using Hop::Object::Component::cTransform;
2121
using Hop::Object::Component::cPhysics;
2222

23+
/**
24+
* @brief A point in a collision mesh.
25+
*
26+
*/
2327
struct MeshPoint
2428
{
29+
/**
30+
* @brief Construct a new Mesh Point from a position and radius.
31+
*
32+
* @param x x coordinate.
33+
* @param y y coordinate.
34+
* @param r radius.
35+
*/
2536
MeshPoint(double x, double y, double r)
2637
: x(x), y(y), r(r)
2738
{}
2839

40+
/**
41+
* @brief Construct a new Mesh Point at (0,0) with radius 0.
42+
*
43+
*/
2944
MeshPoint()
3045
: x(0.0), y(0.0), r(0.0)
3146
{}
@@ -40,16 +55,36 @@ namespace Hop::System::Physics
4055
}
4156
};
4257

58+
/**
59+
* @brief A rectangular mesh point.
60+
*
61+
*/
4362
struct MeshRectangle : public MeshPoint
4463
{
4564

65+
/**
66+
* @brief Construct a new Mesh Rectangle with points at 0.0.
67+
*
68+
*/
4669
MeshRectangle()
4770
: MeshRectangle(0.0, 0.0,
4871
0.0, 0.0,
4972
0.0, 0.0,
5073
0.0, 0.0)
5174
{}
5275

76+
/**
77+
* @brief Construct a new Mesh Rectangle from vertices.
78+
*
79+
* @param llx lower left x.
80+
* @param lly lower left y.
81+
* @param ulx upper left x.
82+
* @param uly upper left y.
83+
* @param urx upper right x.
84+
* @param ury upper right y.
85+
* @param lrx lower right x.
86+
* @param lry lower right y.
87+
*/
5388
MeshRectangle
5489
(
5590
double llx, double lly,
@@ -102,16 +137,24 @@ namespace Hop::System::Physics
102137
double llx, lly, ulx, uly, urx, ury, lrx, lry;
103138
};
104139

140+
/**
141+
* @brief A mesh of collideable primitives.
142+
*
143+
*/
105144
struct CollisionMesh
106145
{
107146
CollisionMesh()
108147
{}
109-
// construct a mesh around a model space polygon
110-
// with vertices v with each mesh vertex having
111-
// radius r in model space
112-
//CollisionMesh(std::vector<Vertex> v, double r = 0.01);
113148

114-
// construct a mesh from given points
149+
/**
150+
* @brief Construct a new Collision Mesh from primitives.
151+
*
152+
* @param v list of collideable primitives.
153+
* @param x x world coordinate of mesh.
154+
* @param y y world coordinate of mesh.
155+
* @param theta orientation of mesh.
156+
* @param scale scale of mesh.
157+
*/
115158
CollisionMesh
116159
(
117160
std::vector<std::shared_ptr<CollisionPrimitive>> v,
@@ -127,6 +170,11 @@ namespace Hop::System::Physics
127170
updateWorldMesh(transform, phys, 0.0);
128171
}
129172

173+
/**
174+
* @brief Construct a new Collision Mesh from a primitives list.
175+
*
176+
* @param v list of collideable primitives.
177+
*/
130178
CollisionMesh
131179
(
132180
std::vector<std::shared_ptr<CollisionPrimitive>> v
@@ -141,6 +189,11 @@ namespace Hop::System::Physics
141189
computeRadius();
142190
}
143191

192+
/**
193+
* @brief Construct a new Collision Mesh from another.
194+
*
195+
* @param m the mesh to copy.
196+
*/
144197
CollisionMesh
145198
(
146199
CollisionMesh & m
@@ -161,6 +214,13 @@ namespace Hop::System::Physics
161214
someRectangles = m.someRectangles;
162215
}
163216

217+
/**
218+
* @brief Construct a new Collision Mesh from model and world vertices.
219+
*
220+
* @param model model positions of mesh primitives.
221+
* @param world world positions of mesh primitives.
222+
* @param tags tags of mesh primitives for sub-meshing.
223+
*/
164224
CollisionMesh
165225
(
166226
std::vector<std::shared_ptr<MeshPoint>> model,
@@ -177,6 +237,12 @@ namespace Hop::System::Physics
177237
updateTags();
178238
}
179239

240+
/**
241+
* @brief Obtain all primitives with the given tag.
242+
*
243+
* @param t tag to select.
244+
* @return (sub) CollisionMesh selected by tag.
245+
*/
180246
CollisionMesh getSubMesh(uint64_t t)
181247
{
182248
auto model = getModelByTag(t);
@@ -185,15 +251,29 @@ namespace Hop::System::Physics
185251
return CollisionMesh(model, world, tags);
186252
}
187253

254+
/**
255+
* @brief Flag mesh as needing an initial update.
256+
*
257+
*/
188258
void reinitialise() { needsInit = true; }
189259

260+
/**
261+
* @brief Set the transform of the mesh.
262+
*
263+
* @param t new transform of the mesh.
264+
*/
190265
void transform(cTransform t)
191266
{
192267
needsInit = true;
193268
cPhysics phys(t.x,t.y,t.theta);
194269
updateWorldMesh(t, phys, 0.0);
195270
}
196271

272+
/**
273+
* @brief Add a primitive to the mesh.
274+
*
275+
* @param c new primitive.
276+
*/
197277
void add(std::shared_ptr<CollisionPrimitive> c)
198278
{
199279

@@ -279,6 +359,11 @@ namespace Hop::System::Physics
279359
needsInit = true;
280360
}
281361

362+
/**
363+
* @brief Remove primitive at index i.
364+
*
365+
* @param i index of primitive to remove.
366+
*/
282367
void remove(size_t i)
283368
{
284369
vertices.erase(vertices.begin()+i);
@@ -290,6 +375,13 @@ namespace Hop::System::Physics
290375
updateTags();
291376
}
292377

378+
/**
379+
* @brief Check if (x,y) hits a primitive.
380+
*
381+
* @param x x coordinate of test point.
382+
* @param y y coordinate of test point.
383+
* @return int index of the first primitive click (or -1 if not clicked).
384+
*/
293385
int clicked(float x, float y)
294386
{
295387

@@ -310,21 +402,47 @@ namespace Hop::System::Physics
310402

311403
size_t size(){return vertices.size();}
312404

405+
/**
406+
* @brief Get a model space primitive.
407+
*
408+
* @param i index of primitive
409+
* @return std::shared_ptr<MeshPoint> model space primitive.
410+
*/
313411
std::shared_ptr<MeshPoint> getModelVertex(size_t i)
314412
{
315413
return vertices[i];
316414
}
317415

416+
/**
417+
* @brief Get a world space primitive.
418+
*
419+
* @param i index of primitive.
420+
* @return std::shared_ptr<CollisionPrimitive> world space primitive.
421+
*/
318422
std::shared_ptr<CollisionPrimitive> getMeshVertex(size_t i)
319423
{
320424
return worldVertices[i];
321425
}
322426

427+
/**
428+
* @brief Get a world space primitive.
429+
*
430+
* @param i index of primitive.
431+
* @return std::shared_ptr<CollisionPrimitive> world space primitive.
432+
*/
323433
std::shared_ptr<CollisionPrimitive> operator[](size_t i)
324434
{
325435
return worldVertices[i];
326436
}
327437

438+
/**
439+
* @brief Update the mesh to a new transform and physics component.
440+
*
441+
* @remark Will integrate mesh points (i.e. handle soft meshes).
442+
* @param transform new transform for the mesh.
443+
* @param physics new physics component for the mesh.
444+
* @param dt timestep.
445+
*/
328446
void updateWorldMesh(
329447
cTransform & transform,
330448
cPhysics & physics,
@@ -342,27 +460,62 @@ namespace Hop::System::Physics
342460
}
343461
}
344462

463+
/**
464+
* @brief Updates the mesh if it is rigid.
465+
*
466+
* @param transform new transform.
467+
* @param dt timestep.
468+
*/
345469
void updateWorldMeshRigid(
346470
const cTransform & transform,
347471
double dt
348472
);
349473

474+
/**
475+
* @brief Update the world mesh if it is soft.
476+
*
477+
* @remark Applies internal soft-body forces.
478+
* @param transform new transform.
479+
* @param physics mesh physics components.
480+
* @param dt timestep.
481+
*/
350482
void updateWorldMeshSoft(
351483
cTransform & transform,
352484
cPhysics & physics,
353485
double dt
354486
);
355487

488+
/**
489+
* @brief The optimal angle to represent the mesh in world space.
490+
*
491+
* @param x x coordinate of the mesh.
492+
* @param y y coordinate of the mesh.
493+
* @param scaleX scale of mesh in x.
494+
* @param scaleY scale of mesh in y.
495+
* @return double optimal orientation.
496+
*/
356497
double bestAngle(double x, double y, double scaleX, double scaleY);
498+
357499
void centerOfMassWorld(double & cx, double & cy);
500+
501+
/**
502+
* @brief Rebase coordinates to centre of mess.
503+
*
504+
*/
358505
void modelToCenterOfMassFrame();
359506

360507
double momentOfInertia(double x, double y, double mass);
508+
361509
void computeRadius();
510+
362511
double getRadius(){return radius;}
363512

364513
bool getIsRigid(){ return isRigid; }
365514

515+
/**
516+
* @brief Determine if all mesh points are rigid.
517+
*
518+
*/
366519
void calculateIsRigid()
367520
{
368521
for (auto v : worldVertices)
@@ -425,6 +578,12 @@ namespace Hop::System::Physics
425578
}
426579
}
427580

581+
/**
582+
* @brief Get all world space primitives by tag.
583+
*
584+
* @param t tag to match.
585+
* @return std::vector<std::shared_ptr<CollisionPrimitive>> world primitives matching the tag.
586+
*/
428587
std::vector<std::shared_ptr<CollisionPrimitive>> getByTag(uint64_t t)
429588
{
430589
std::vector<std::shared_ptr<CollisionPrimitive>> v;
@@ -444,6 +603,12 @@ namespace Hop::System::Physics
444603
return v;
445604
}
446605

606+
/**
607+
* @brief Get all model space primitives by tag.
608+
*
609+
* @param t tag to select.
610+
* @return std::vector<std::shared_ptr<MeshPoint>> model primitives matching tag.
611+
*/
447612
std::vector<std::shared_ptr<MeshPoint>> getModelByTag(uint64_t t)
448613
{
449614
std::vector<std::shared_ptr<MeshPoint>> v;
@@ -463,6 +628,11 @@ namespace Hop::System::Physics
463628
return v;
464629
}
465630

631+
/**
632+
* @brief Remove all primitives matching tag.
633+
*
634+
* @param t tag to match.
635+
*/
466636
void removeByTag(uint64_t t)
467637
{
468638
std::vector<std::shared_ptr<CollisionPrimitive>> v;
@@ -486,11 +656,22 @@ namespace Hop::System::Physics
486656
}
487657
}
488658

659+
/**
660+
* @brief Get the world-space bounding box.
661+
*
662+
* @return Hop::Maths::BoundingBox
663+
*/
489664
Hop::Maths::BoundingBox getBoundingBox() const
490665
{
491666
return getBoundingBox(worldVertices);
492667
}
493668

669+
/**
670+
* @brief Get the bounding box of a list of primitives.
671+
*
672+
* @param c primitives to bound.
673+
* @return Hop::Maths::BoundingBox
674+
*/
494675
Hop::Maths::BoundingBox getBoundingBox
495676
(
496677
const std::vector<std::shared_ptr<CollisionPrimitive>> & c
@@ -518,6 +699,12 @@ namespace Hop::System::Physics
518699
return Hop::Maths::boundingBox(v, r);
519700
}
520701

702+
/**
703+
* @brief Get the bounding box of all primitives with tag.
704+
*
705+
* @param tag tag to match.
706+
* @return Hop::Maths::BoundingBox
707+
*/
521708
Hop::Maths::BoundingBox getBoundingBox(uint64_t tag)
522709
{
523710
std::vector<std::shared_ptr<CollisionPrimitive>> c = getByTag(tag);

0 commit comments

Comments
 (0)