-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from notgiven688/prog
- Loading branch information
Showing
7 changed files
with
187 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright (c) Thorben Linneweber and others | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
using System; | ||
using Jitter2.LinearMath; | ||
|
||
namespace Jitter2.Collision.Shapes; | ||
|
||
/// <summary> | ||
/// Represents a single triangle within a mesh. The triangle is not flat but extends | ||
/// along its negative normal direction. This extension gives the triangle thickness, | ||
/// which can be controlled by the <see cref="Thickness"/> parameter. | ||
/// </summary> | ||
public class FatTriangleShape : TriangleShape | ||
{ | ||
private float thickness; | ||
|
||
/// <summary> | ||
/// Set or get the thickness of the triangle. | ||
/// </summary> | ||
/// <exception cref="ArgumentException">Thickness must be larger than 0.01 length units.</exception> | ||
public float Thickness | ||
{ | ||
get => thickness; | ||
set | ||
{ | ||
const float minimumThickness = 0.01f; | ||
|
||
if (value < minimumThickness) | ||
{ | ||
throw new ArgumentException($"{nameof(Thickness)} must not be smaller than {minimumThickness}"); | ||
} | ||
|
||
thickness = value; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the TriangleShape class. | ||
/// </summary> | ||
/// <param name="mesh">The triangle mesh to which this triangle belongs.</param> | ||
/// <param name="index">The index representing the position of the triangle within the mesh.</param> | ||
public FatTriangleShape(TriangleMesh mesh, int index, float thickness = 0.2f) : base(mesh, index) | ||
{ | ||
this.thickness = thickness; | ||
UpdateWorldBoundingBox(); | ||
} | ||
|
||
public override void CalculateBoundingBox(in JQuaternion orientation, in JVector position, out JBBox box) | ||
{ | ||
ref var triangle = ref Mesh.Indices[Index]; | ||
var a = Mesh.Vertices[triangle.IndexA]; | ||
var b = Mesh.Vertices[triangle.IndexB]; | ||
var c = Mesh.Vertices[triangle.IndexC]; | ||
|
||
JVector.Transform(a, orientation, out a); | ||
JVector.Transform(b, orientation, out b); | ||
JVector.Transform(c, orientation, out c); | ||
|
||
JVector delta = JVector.Normalize((a - b) % (a - c)) * Thickness; | ||
|
||
box = JBBox.SmallBox; | ||
|
||
box.AddPoint(a); | ||
box.AddPoint(b); | ||
box.AddPoint(c); | ||
|
||
box.AddPoint(a + delta); | ||
box.AddPoint(b + delta); | ||
box.AddPoint(c + delta); | ||
} | ||
|
||
public override void SupportMap(in JVector direction, out JVector result) | ||
{ | ||
ref var triangle = ref Mesh.Indices[Index]; | ||
|
||
JVector a = Mesh.Vertices[triangle.IndexA]; | ||
JVector b = Mesh.Vertices[triangle.IndexB]; | ||
JVector c = Mesh.Vertices[triangle.IndexC]; | ||
|
||
float min = JVector.Dot(a, direction); | ||
float dot = JVector.Dot(b, direction); | ||
|
||
result = a; | ||
|
||
if (dot > min) | ||
{ | ||
min = dot; | ||
result = b; | ||
} | ||
|
||
dot = JVector.Dot(c, direction); | ||
|
||
if (dot > min) | ||
{ | ||
result = c; | ||
} | ||
|
||
JVector nnorm = (a - b) % (a - c); | ||
|
||
if (JVector.Dot(nnorm, direction) > 0.0f) | ||
result += JVector.Normalize(nnorm) * Thickness; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters