1 /*
2 Copyright (c) 2014 Timur Gafarov 
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module dmech.shape;
30 
31 import dlib.math.vector;
32 import dlib.math.matrix;
33 import dlib.math.affine;
34 import dlib.geometry.aabb;
35 import dlib.geometry.sphere;
36 
37 import dmech.geometry;
38 
39 /*
40  * ShapeComponent is a proxy object between RigidBody and Geometry.
41  * It stores non-geometric information such as mass contribution,
42  * position in body space and a unique identifier for indexing in
43  * contact cache.
44  */
45 
46 class ShapeComponent
47 {
48     Geometry geometry; // geometry
49     Vector3f centroid; // position in body space
50     float mass;        // mass contribution
51     uint id = 0;       // global identifier
52 
53     Matrix4x4f _transformation;
54 
55     alias geometry this;
56 
57     bool locked = false;
58 
59     @property
60     {
61         Matrix4x4f transformation()
62         {
63             while(locked) {}
64             return _transformation;
65         }
66 
67         void transformation(Matrix4x4f m)
68         {
69             locked = true;
70             _transformation = m;
71             locked = false;
72         }
73     }
74 
75     this(Geometry g, Vector3f c, float m)
76     {
77         geometry = g;
78         centroid = c;
79         mass = m;
80 
81         _transformation = Matrix4x4f.identity;
82     }
83 
84     // position in world space
85     @property Vector3f position()
86     {
87         return _transformation.translation;
88     }
89 
90     @property AABB boundingBox()
91     {
92         return geometry.boundingBox(
93             _transformation.translation);
94     }
95     
96     @property Sphere boundingSphere()
97     {
98         AABB aabb = geometry.boundingBox(
99             _transformation.translation);
100         return Sphere(aabb.center, aabb.size.length);
101     }
102 
103     Vector3f supportPointGlobal(Vector3f dir)
104     {
105         Vector3f result;
106         Matrix4x4f* m = &_transformation;
107 
108         result.x = ((dir.x * m.a11) + (dir.y * m.a21)) + (dir.z * m.a31);
109         result.y = ((dir.x * m.a12) + (dir.y * m.a22)) + (dir.z * m.a32);
110         result.z = ((dir.x * m.a13) + (dir.y * m.a23)) + (dir.z * m.a33);
111 
112         result = geometry.supportPoint(result);
113 
114         float x = ((result.x * m.a11) + (result.y * m.a12)) + (result.z * m.a13);
115         float y = ((result.x * m.a21) + (result.y * m.a22)) + (result.z * m.a23);
116         float z = ((result.x * m.a31) + (result.y * m.a32)) + (result.z * m.a33);
117 
118         result.x = m.a14 + x;
119         result.y = m.a24 + y;
120         result.z = m.a34 + z;
121 
122         return result;
123     }
124 }
125