MEPP2 Project
tangents.hpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as
6 // published by the Free Software Foundation; either version 3 of
7 // the License, or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 #pragma once
12 
13 #include <cmath>
14 
15 namespace FEVV {
16 namespace Operators {
17 
31 template< typename Point, typename Vector >
32 Vector
34  const Point &pt2,
35  const Point &pt3,
36  const Vector &uv1,
37  const Vector &uv2,
38  const Vector &uv3)
39 {
40  const Vector pos1(pt1[0], pt1[1], pt1[2]);
41  const Vector pos2(pt2[0], pt2[1], pt2[2]);
42  const Vector pos3(pt3[0], pt3[1], pt3[2]);
43 
44  const Vector first_edge = pos2 - pos1;
45  const Vector second_edge = pos3 - pos1;
46 
47  Vector first_uv_diff = uv2 - uv1;
48  Vector second_uv_diff = uv3 - uv1;
49 
50  // RM: fix for black areas, which happens when UVs are equal;
51  // manually forcing UVs to be different from each other
52  if(std::abs(first_uv_diff[0]) <= 0.01f && std::abs(first_uv_diff[1]) <= 0.01f)
53  {
54  const Vector uv2_fix(uv2[0] + 0.0001, uv2[1], uv2[2]);
55  first_uv_diff = uv2_fix - uv1;
56  }
57 
58  if(std::abs(second_uv_diff[0]) <= 0.01f &&
59  std::abs(second_uv_diff[1]) <= 0.01f)
60  {
61  const Vector uv3_fix(uv3[0], uv3[1] + 0.0001, uv3[2]);
62  second_uv_diff = uv3_fix - uv1;
63  }
64 
65  // RM: calculate tangent from positions & UV space
66  const float inversion_factor = 1.f / (first_uv_diff[0] * second_uv_diff[1] -
67  second_uv_diff[0] * first_uv_diff[1]);
68 
69  const Vector tangent =
70  (first_edge * second_uv_diff[1] - second_edge * first_uv_diff[1]) *
71  inversion_factor;
72 
73  return tangent;
74 }
75 
76 } // namespace Operators
77 } // namespace FEVV
FEVV
Interfaces for plugins These interfaces will be used for different plugins.
Definition: Assert.h:16
FEVV::DataStructures::AIF::AIFVector
Definition: AIFProperties.h:173
FEVV::Operators::calculate_triangle_tangent
Vector calculate_triangle_tangent(const Point &pt1, const Point &pt2, const Point &pt3, const Vector &uv1, const Vector &uv2, const Vector &uv3)
Calculate the actual face tangent from three connected vertices' positions & texture coordinates.
Definition: tangents.hpp:33
FEVV::DataStructures::AIF::AIFPoint
Definition: AIFProperties.h:31