1 /* 2 Copyright (c) 2011-2021 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 /** 30 * Copyright: Timur Gafarov 2011-2021. 31 * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 32 * Authors: Timur Gafarov 33 */ 34 module dlib.geometry.utils; 35 36 private 37 { 38 import dlib.math.vector; 39 } 40 41 Vector3f triBarycentricCoords(Vector3f v0, Vector3f v1, Vector3f v2, Vector3f p) 42 { 43 float triArea = cross((v1 - v0), (v2 - v0)).length * 0.5f; 44 float u = (cross((v1 - p ), (v2 - p)).length * 0.5f) / triArea; 45 float v = (cross((v0 - p ), (v2 - p)).length * 0.5f) / triArea; 46 float w = (cross((v0 - p ), (v1 - p)).length * 0.5f) / triArea; 47 return Vector3f(u, v, w); 48 } 49 50 // ta, tb, tc - triangle texture coords 51 // s, t - coords in texture space 52 Vector3f triBarycentricCoords(Vector2f ta, Vector2f tb, Vector2f tc, float s, float t) 53 { 54 float d = (tb.x * tc.y) - (tb.y * tc.x) - (ta.x * tc.y) + (ta.y * tc.x) + (ta.x * tb.y) - (ta.y * tb.x); 55 56 float m1 = ((tb.x * tc.y) - (tb.y * tc.x) - (s * tc.y) + (t * tc.x) + (s * tb.y) - (t * tb.x)) / d; 57 float m2 = ((s * tc.y) - (t * tc.x) - (ta.x * tc.y) + (ta.y * tc.x) + (ta.x * t) - (ta.y * s)) / d; 58 float m3 = ((tb.x * t) - (tb.y * s) - (ta.x * t) + (ta.y * s) + (ta.x * tb.y) - (ta.y * tb.x)) / d; 59 60 return Vector3f(m1, m2, m3); 61 } 62 63 // va, vb, vc - triangle vectors (vertices, normals, colors, etc) 64 // bcc - barycentric coords 65 Vector3f triTextureSpaceToObjectSpace(Vector3f va, Vector3f vb, Vector3f vc, Vector3f bcc) 66 { 67 return Vector3f( 68 va.x * bcc.x + vb.x * bcc.y + vc.x * bcc.z, 69 va.y * bcc.x + vb.y * bcc.y + vc.y * bcc.z, 70 va.z * bcc.x + vb.z * bcc.y + vc.z * bcc.z, 71 ); 72 } 73 74 Vector2f triObjectSpaceToTextureSpace( 75 Vector3f p1, Vector3f p2, Vector3f p3, 76 Vector2f uv1, Vector2f uv2, Vector2f uv3, 77 Vector3f pos) 78 { 79 // Compute vectors 80 Vector3f v0 = p3 - p1; 81 Vector3f v1 = p2 - p1; 82 Vector3f v2 = pos - p1; 83 84 // Compute dot products 85 float dot00 = dot(v0, v0); 86 float dot01 = dot(v0, v1); 87 float dot02 = dot(v0, v2); 88 float dot11 = dot(v1, v1); 89 float dot12 = dot(v1, v2); 90 91 // Compute barycentric coordinates 92 float invDenom = 1.0f / (dot00 * dot11 - dot01 * dot01); 93 float u = (dot11 * dot02 - dot01 * dot12) * invDenom; 94 float v = (dot00 * dot12 - dot01 * dot02) * invDenom; 95 96 Vector2f t2 = uv2 - uv1; 97 Vector2f t1 = uv3 - uv1; 98 99 return uv1 + t1*u + t2*v; 100 } 101 102 float sign(Vector2f p1, Vector2f p2, Vector2f p3) 103 { 104 return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y); 105 } 106 107 bool isPointInTriangle2D(Vector2f pt, Vector2f v1, Vector2f v2, Vector2f v3) 108 { 109 bool b1, b2, b3; 110 111 b1 = sign(pt, v1, v2) < 0.0f; 112 b2 = sign(pt, v2, v3) < 0.0f; 113 b3 = sign(pt, v3, v1) < 0.0f; 114 115 return ((b1 == b2) && (b2 == b3)); 116 }