src/geometry/plane.cpp

Sun, 10 Jun 2018 23:07:24 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 10 Jun 2018 23:07:24 +0300
changeset 1401
59b578c77111
parent 1390
3eace926af7f
permissions
-rw-r--r--

fixed bugs regarding circular primitives

1390
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2013 - 2018 Teemu Piippo
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #include "plane.h"
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 /*
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 * Constructs a plane from three points
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 */
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 Plane Plane::fromPoints(const Vertex& v1, const Vertex& v2, const Vertex& v3)
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 {
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 Plane result;
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 result.normal = QVector3D::crossProduct(v2 - v1, v3 - v1);
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 result.point = v1;
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 return result;
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 }
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 /*
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 * Returns whether or not this plane is valid.
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 */
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 bool Plane::isValid() const
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 {
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 return not normal.isNull();
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 }
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 /*
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 * Finds the intersection of a line and a plane. Returns a structure containing whether or not
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 * there was an intersection as well as the intersected vertex if there was one. Can return a point
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 * outside of the line segment.
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 *
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 * C.f. https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection#Algebraic_form
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 */
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 Plane::IntersectionResult Plane::intersection(const Line& line) const
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 {
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 if (isValid())
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 {
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 QVector3D lineVector = line.v_2 - line.v_1;
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 qreal dot = QVector3D::dotProduct(lineVector, normal);
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 if (not qFuzzyCompare(dot, 0.0))
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 {
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 qreal factor = QVector3D::dotProduct(point - line.v_1, normal) / dot;
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 IntersectionResult result;
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 result.intersected = true;
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 result.vertex = Vertex::fromVector(lineVector * factor + (line.v_1 - Vertex {0, 0, 0}));
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 return result;
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 }
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 else
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 {
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 // did not intersect
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 return {};
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 }
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 }
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 else
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 {
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 // plane is invalid
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 return {};
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 }
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 }

mercurial