sculpto
cone.cpp
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file cone.cpp
3 * \brief Topology cone object class implementation module.
4 *
5 * \author Sabitov Kirill
6 * \date 05 July 2022
7 *********************************************************************/
8
9#include "sclpch.h"
10#include "cone.h"
11
12scl::topology::cone::cone(const vec3 &A, float R1, const vec3 &B, float R2, int Slices) :
13 grid(1, Slices)
14{
15 float height = (A - B).Length();
16 vec3 height_dir = (A - B).Normalized();
17 vec3 height_dir_90 = vec3 { -height_dir.Y, height_dir.X, height_dir.Z };
18 vec3 guide = height_dir_90 - height_dir;
19 float cos_alpha = height_dir_90.Dot(guide);
20 float sin_alpha = sqrt(1 - cos_alpha * cos_alpha);
21 vec3 normal = vec3(cos_alpha, sin_alpha, 0).Normalized();
22
23 for (int i = 0; i <= Slices; i++)
24 {
25 float t = (float)i / Slices;
26 float phi = 360 * t;
27 float x = sin(phi);
28 float z = cos(phi);
29 matr4 rotation = matr4::Rotate(height_dir, phi);
30 vec3 normal = rotation.TransformVector(normal);
31 vec3 tangent = vec3 { z, 0, -x };
32 vec3 bitangent = tangent.Cross(normal);
33
34 Vertices.emplace_back(
35 A + rotation.TransformVector(height_dir_90 * R1),
36 normal, tangent, bitangent, vec2 { t, 0 }
37 );
38 Vertices.emplace_back(
39 B + rotation.TransformVector(height_dir_90 * R2),
40 normal, tangent, bitangent, vec2 { t, 1 }
41 );
42 }
43}
static matr4 Rotate(vec3< float > Axis, degrees< float > Angle)
Definition: matr4.h:288
vec3< T > TransformVector(const vec3< T > &V) const
Definition: matr4.h:513
vec3 Normalized() const
Definition: vec3.h:190
T Dot(const vec3 &Other) const
Definition: vec3.h:213
vec3 Cross(const vec3 &Other) const
Definition: vec3.h:224
std::vector< vertex > Vertices
Definition: basis.h:22
cone(const vec3 &A, float R1, const vec3 &B, float R2, int Slices)
Definition: cone.cpp:12
Topology cone object class definition module.
math::vec3< float > vec3
Definition: base.h:38
Sculpto library prehompiled header. Defines common definitions, includes commonly used modules.