Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOTriangle.h
Go to the documentation of this file.
1/*
2
3OOTriangle.h
4
5Mathematical framework for Oolite.
6
7Oolite
8Copyright (C) 2004-2013 Giles C Williams and contributors
9
10This program is free software; you can redistribute it and/or
11modify it under the terms of the GNU General Public License
12as published by the Free Software Foundation; either version 2
13of the License, or (at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23MA 02110-1301, USA.
24
25*/
26
27
28#ifndef INCLUDED_OOMATHS_h
29 #error Do not include OOTriangle.h directly; include OOMaths.h.
30#else
31
32
33typedef struct
34{
35 Vector v[3];
36} Triangle;
37
38
39/* Calculate normal for triangle. */
40OOINLINE Vector calculateNormalForTriangle(Triangle *ioTriangle) NONNULL_FUNC;
41
42/* Generate a triangle from three vertices. */
43OOINLINE Triangle make_triangle(Vector v0, Vector v1, Vector v2) CONST_FUNC;
44
45/* resolve vector in arbitrary ijk vectors */
46OOINLINE Vector resolveVectorInIJK(Vector v0, Triangle ijk);
47
48/* Test whether triangle's area is 0. */
49OOINLINE bool OOTriangleIsDegenerate(Triangle tri) CONST_FUNC;
50
51
52/*** Only inline definitions beyond this point ***/
53
54OOINLINE Triangle make_triangle(Vector v0, Vector v1, Vector v2)
55{
56 return (Triangle){{ v0, v1, v2 }};
57}
58
59
60OOINLINE Vector calculateNormalForTriangle(Triangle *tri)
61{
62 Vector v01 = vector_subtract(tri->v[1], tri->v[0]);
63 Vector v12 = vector_subtract(tri->v[2], tri->v[1]);
64 return cross_product(v01, v12);
65}
66
67
68OOINLINE Vector resolveVectorInIJK(Vector v0, Triangle ijk)
69{
70 Vector result;
71 result.x = dot_product(v0, ijk.v[0]);
72 result.y = dot_product(v0, ijk.v[1]);
73 result.z = dot_product(v0, ijk.v[2]);
74 return result;
75}
76
77
78OOINLINE bool OOTriangleIsDegenerate(Triangle tri)
79{
80 return vector_equal(tri.v[0], tri.v[1]) ||
81 vector_equal(tri.v[1], tri.v[2]) ||
82 vector_equal(tri.v[2], tri.v[0]);
83}
84
85
86#endif /* INCLUDED_OOMATHS_h */
#define NONNULL_FUNC
#define OOINLINE
#define CONST_FUNC