Skip to content

Commit

Permalink
Merge pull request #1338 from vsg-dev/CoordinateFrame_long_double
Browse files Browse the repository at this point in the history
vsg::CoordinateFrame and long double support
  • Loading branch information
robertosfield authored Nov 29, 2024
2 parents dfa2ff5 + cbdc239 commit a7a7db7
Show file tree
Hide file tree
Showing 34 changed files with 468 additions and 64 deletions.
1 change: 1 addition & 0 deletions cmake/cppcheck-suppression-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ shadowFunction:*/src/vsg/state/DescriptorImage.cpp
shadowFunction:*/src/vsg/utils/FindDynamicObjects.cpp
shadowFunction:*/src/vsg/utils/LoadPagedLOD.cpp
shadowFunction:*/src/vsg/utils/PropagateDynamicObjects.cpp
shadowFunction:*/src/vsg/app/ViewMatrix.cpp

// suppress unhelpful warning of c casts
cstyleCast:*/include/vsg/io/mem_stream.h
Expand Down
1 change: 1 addition & 0 deletions include/vsg/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/nodes/AbsoluteTransform.h>
#include <vsg/nodes/Bin.h>
#include <vsg/nodes/Compilable.h>
#include <vsg/nodes/CoordinateFrame.h>
#include <vsg/nodes/CullGroup.h>
#include <vsg/nodes/CullNode.h>
#include <vsg/nodes/DepthSorted.h>
Expand Down
2 changes: 2 additions & 0 deletions include/vsg/app/RecordTraversal.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace vsg
class Layer;
class Transform;
class MatrixTransform;
class CoordinateFrame;
class Joint;
class TileDatabase;
class VertexDraw;
Expand Down Expand Up @@ -135,6 +136,7 @@ namespace vsg
// transform nodes
void apply(const Transform& transform);
void apply(const MatrixTransform& mt);
void apply(const CoordinateFrame& cf);

// Animation nodes
void apply(const Joint& joint);
Expand Down
69 changes: 46 additions & 23 deletions include/vsg/app/ViewMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ namespace vsg
{
}

virtual dmat4 transform() const = 0;
/// origin value provides a means of translating the view matrix relative to the origin of any CoordinateFrame subgraphs
/// to maximize the precision when moving around the CoordinateFrame subgraph. This is helpful for astronmically large
/// scenes where standrd double precision is insufficient for avoiding visually significant numerical errors.
dvec3 origin;

virtual dmat4 inverse() const
virtual dmat4 transform(const dvec3& offset = {}) const = 0;

virtual dmat4 inverse(const dvec3& offset = {}) const
{
return vsg::inverse(transform());
return vsg::inverse(transform(offset));
}

void read(Input& input) override;
void write(Output& output) const override;
};
VSG_type_name(vsg::ViewMatrix);

Expand Down Expand Up @@ -79,21 +87,11 @@ namespace vsg

ref_ptr<Object> clone(const CopyOp& copyop = {}) const override { return LookAt::create(*this, copyop); }

void transform(const dmat4& matrix)
{
up = normalize(matrix * (eye + up) - matrix * eye);
center = matrix * center;
eye = matrix * eye;
}
void transform(const dmat4& matrix);

void set(const dmat4& matrix)
{
up = normalize(matrix * (dvec3(0.0, 0.0, 0.0) + dvec3(0.0, 1.0, 0.0)) - matrix * dvec3(0.0, 0.0, 0.0));
center = matrix * dvec3(0.0, 0.0, -1.0);
eye = matrix * dvec3(0.0, 0.0, 0.0);
}
void set(const dmat4& matrix);

dmat4 transform() const override { return lookAt(eye, center, up); }
dmat4 transform(const dvec3& offset = {}) const override;

void read(Input& input) override;
void write(Output& output) const override;
Expand All @@ -104,8 +102,36 @@ namespace vsg
};
VSG_type_name(vsg::LookAt);

/// LookDirection is a ViewMatrix that uses a position and rotation to set the view matrix.
class VSG_DECLSPEC LookDirection : public vsg::Inherit<ViewMatrix, LookDirection>
{
public:
LookDirection() :
position(0.0, 0.0, 0.0),
rotation()
{
}

LookDirection(const LookDirection& view, const CopyOp& copyop = {}) :
Inherit(view, copyop),
position(view.position),
rotation(view.rotation)
{
}

ref_ptr<Object> clone(const CopyOp& copyop = {}) const override { return LookDirection::create(*this, copyop); }

dvec3 position;
dquat rotation;

void set(const dmat4& matrix);

dmat4 transform(const dvec3& offset = {}) const override;
};
VSG_type_name(vsg::LookDirection);

/// RelativeViewMatrix is a ViewMatrix that decorates another ViewMatrix and pre-multiplies its transform matrix to give a relative view matrix.
class RelativeViewMatrix : public Inherit<ViewMatrix, RelativeViewMatrix>
class VSG_DECLSPEC RelativeViewMatrix : public Inherit<ViewMatrix, RelativeViewMatrix>
{
public:
RelativeViewMatrix(const dmat4& m, ref_ptr<ViewMatrix> vm) :
Expand All @@ -115,10 +141,7 @@ namespace vsg
}

/// returns matrix * viewMatrix->transform()
dmat4 transform() const override
{
return matrix * viewMatrix->transform();
}
dmat4 transform(const dvec3& offset = {}) const override;

dmat4 matrix;
ref_ptr<ViewMatrix> viewMatrix;
Expand All @@ -141,8 +164,8 @@ namespace vsg
objectPath(path.begin(), path.end()) {}

/// returns matrix * computeTransfrom(objectPath)
dmat4 transform() const override;
dmat4 inverse() const override;
dmat4 transform(const dvec3& offset = {}) const override;
dmat4 inverse(const dvec3& offset = {}) const override;

dmat4 matrix;
RefObjectPath objectPath;
Expand Down
6 changes: 4 additions & 2 deletions include/vsg/core/ConstVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ namespace vsg
class StateGroup;
class CullGroup;
class CullNode;
class MatrixTransform;
class Transform;
class MatrixTransform;
class CoordinateFrame;
class Geometry;
class VertexDraw;
class VertexIndexDraw;
Expand Down Expand Up @@ -323,8 +324,9 @@ namespace vsg
virtual void apply(const StateGroup&);
virtual void apply(const CullGroup&);
virtual void apply(const CullNode&);
virtual void apply(const MatrixTransform&);
virtual void apply(const Transform&);
virtual void apply(const MatrixTransform&);
virtual void apply(const CoordinateFrame&);
virtual void apply(const Geometry&);
virtual void apply(const VertexDraw&);
virtual void apply(const VertexIndexDraw&);
Expand Down
6 changes: 4 additions & 2 deletions include/vsg/core/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ namespace vsg
class StateGroup;
class CullGroup;
class CullNode;
class MatrixTransform;
class Transform;
class MatrixTransform;
class CoordinateFrame;
class Geometry;
class VertexDraw;
class VertexIndexDraw;
Expand Down Expand Up @@ -323,8 +324,9 @@ namespace vsg
virtual void apply(StateGroup&);
virtual void apply(CullGroup&);
virtual void apply(CullNode&);
virtual void apply(MatrixTransform&);
virtual void apply(Transform&);
virtual void apply(MatrixTransform&);
virtual void apply(CoordinateFrame&);
virtual void apply(Geometry&);
virtual void apply(VertexDraw&);
virtual void apply(VertexIndexDraw&);
Expand Down
1 change: 1 addition & 0 deletions include/vsg/io/AsciiInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ namespace vsg
void read(size_t num, uint64_t* value) override { _read(num, value); }
void read(size_t num, float* value) override { _read(num, value); }
void read(size_t num, double* value) override { _read(num, value); }
void read(size_t num, long double* value) override { _read(num, value); }

// read in an individual string
void _read(std::string& value);
Expand Down
6 changes: 6 additions & 0 deletions include/vsg/io/AsciiOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ namespace vsg
_output.precision(double_precision);
_write_real(num, value);
}
void write(size_t num, const long double* value) override
{
_output.precision(long_double_precision);
_write_real(num, value);
}

void _write(const std::string& str)
{
Expand All @@ -161,6 +166,7 @@ namespace vsg

int float_precision = 6;
int double_precision = 12;
int long_double_precision = 24;

protected:
std::ostream& _output;
Expand Down
1 change: 1 addition & 0 deletions include/vsg/io/BinaryInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace vsg
void read(size_t num, uint64_t* value) override { _read(num, value); }
void read(size_t num, float* value) override { _read(num, value); }
void read(size_t num, double* value) override { _read(num, value); }
void read(size_t num, long double* value) override;

// read in an individual string
void _read(std::string& value);
Expand Down
1 change: 1 addition & 0 deletions include/vsg/io/BinaryOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace vsg
void write(size_t num, const uint64_t* value) override { _write(num, value); }
void write(size_t num, const float* value) override { _write(num, value); }
void write(size_t num, const double* value) override { _write(num, value); }
void write(size_t num, const long double* value) override;

void _write(const std::string& str);
void _write(const std::wstring& str);
Expand Down
4 changes: 4 additions & 0 deletions include/vsg/io/Input.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace vsg
virtual void read(size_t num, uint64_t* value) = 0;
virtual void read(size_t num, float* value) = 0;
virtual void read(size_t num, double* value) = 0;
virtual void read(size_t num, long double* value) = 0;
virtual void read(size_t num, std::string* value) = 0;
virtual void read(size_t num, std::wstring* value) = 0;
virtual void read(size_t num, Path* value) = 0;
Expand All @@ -78,6 +79,9 @@ namespace vsg
void read(size_t num, vec4* value) { read(num * value->size(), value->data()); }
void read(size_t num, dvec2* value) { read(num * value->size(), value->data()); }
void read(size_t num, dvec3* value) { read(num * value->size(), value->data()); }
void read(size_t num, ldvec4* value) { read(num * value->size(), value->data()); }
void read(size_t num, ldvec2* value) { read(num * value->size(), value->data()); }
void read(size_t num, ldvec3* value) { read(num * value->size(), value->data()); }
void read(size_t num, dvec4* value) { read(num * value->size(), value->data()); }
void read(size_t num, bvec2* value) { read(num * value->size(), value->data()); }
void read(size_t num, bvec3* value) { read(num * value->size(), value->data()); }
Expand Down
4 changes: 4 additions & 0 deletions include/vsg/io/Output.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace vsg
virtual void write(size_t num, const uint64_t* value) = 0;
virtual void write(size_t num, const float* value) = 0;
virtual void write(size_t num, const double* value) = 0;
virtual void write(size_t num, const long double* value) = 0;
virtual void write(size_t num, const std::string* value) = 0;
virtual void write(size_t num, const std::wstring* value) = 0;
virtual void write(size_t num, const Path* value) = 0;
Expand All @@ -79,6 +80,9 @@ namespace vsg
void write(size_t num, const dvec2* value) { write(num * value->size(), value->data()); }
void write(size_t num, const dvec3* value) { write(num * value->size(), value->data()); }
void write(size_t num, const dvec4* value) { write(num * value->size(), value->data()); }
void write(size_t num, const ldvec2* value) { write(num * value->size(), value->data()); }
void write(size_t num, const ldvec3* value) { write(num * value->size(), value->data()); }
void write(size_t num, const ldvec4* value) { write(num * value->size(), value->data()); }
void write(size_t num, const bvec2* value) { write(num * value->size(), value->data()); }
void write(size_t num, const bvec3* value) { write(num * value->size(), value->data()); }
void write(size_t num, const bvec4* value) { write(num * value->size(), value->data()); }
Expand Down
6 changes: 4 additions & 2 deletions include/vsg/maths/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ namespace vsg
}
};

using box = t_box<float>; /// float box class
using dbox = t_box<double>; /// double box class
using box = t_box<float>; /// float box class
using dbox = t_box<double>; /// double box class
using ldbox = t_box<long double>; /// double box class

VSG_type_name(vsg::box);
VSG_type_name(vsg::dbox);
VSG_type_name(vsg::ldbox);

template<typename T>
constexpr bool operator==(const t_box<T>& lhs, const t_box<T>& rhs)
Expand Down
3 changes: 3 additions & 0 deletions include/vsg/maths/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ namespace vsg
return start * one_minus_r + end * r;
}

/// return the number of bits supported by the long double implementation - VisualStudio by default 64 bits, stored as 8 bytes, & Linux x86_64 defaults to 80 bits stored as 16bytes, some CPU achitectures support full 128 bits/16 bytes.
extern VSG_DECLSPEC uint32_t native_long_double_bits();

} // namespace vsg
6 changes: 4 additions & 2 deletions include/vsg/maths/mat4.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,13 @@ namespace vsg
const T* data() const { return value[0].data(); }
};

using mat4 = t_mat4<float>; /// float 4x4 matrix
using dmat4 = t_mat4<double>; /// double 4x4 matrix
using mat4 = t_mat4<float>; /// float 4x4 matrix
using dmat4 = t_mat4<double>; /// double 4x4 matrix
using ldmat4 = t_mat4<long double>; /// long double 4x4 matrix

VSG_type_name(vsg::mat4);
VSG_type_name(vsg::dmat4);
VSG_type_name(vsg::ldmat4);

template<typename T>
bool operator==(const t_mat4<T>& lhs, const t_mat4<T>& rhs)
Expand Down
1 change: 1 addition & 0 deletions include/vsg/maths/plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ namespace vsg

using plane = t_plane<float>;
using dplane = t_plane<double>;
using ldplane = t_plane<long double>;

VSG_type_name(vsg::plane);
VSG_type_name(vsg::dplane);
Expand Down
6 changes: 4 additions & 2 deletions include/vsg/maths/quat.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ namespace vsg
explicit operator bool() const noexcept { return value[0] != 0.0 || value[1] != 0.0 || value[2] != 0.0 || value[3] != 0.0; }
};

using quat = t_quat<float>; /// float quaternion
using dquat = t_quat<double>; /// double quaternion
using quat = t_quat<float>; /// float quaternion
using dquat = t_quat<double>; /// double quaternion
using ldquat = t_quat<long double>; /// long double quaternion

VSG_type_name(vsg::quat);
VSG_type_name(vsg::dquat);
VSG_type_name(vsg::ldquat);

template<typename T>
constexpr bool operator==(const t_quat<T>& lhs, const t_quat<T>& rhs)
Expand Down
6 changes: 4 additions & 2 deletions include/vsg/maths/sphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ namespace vsg
}
};

using sphere = t_sphere<float>; /// float sphere class
using dsphere = t_sphere<double>; /// double sphere class
using sphere = t_sphere<float>; /// float sphere class
using dsphere = t_sphere<double>; /// double sphere class
using ldsphere = t_sphere<long double>; /// long double sphere class

VSG_type_name(vsg::sphere);
VSG_type_name(vsg::dsphere);
VSG_type_name(vsg::ldsphere);

template<typename T>
constexpr bool operator==(const t_sphere<T>& lhs, const t_sphere<T>& rhs)
Expand Down
5 changes: 5 additions & 0 deletions include/vsg/maths/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ namespace vsg
/// assumes matrix has no skew or perspective components
extern VSG_DECLSPEC bool decompose(const dmat4& m, dvec3& translation, dquat& rotation, dvec3& scale);

/// decompose long double matrix into translation, rotation and scale components.
/// maps to TRS form: vsg::translate(translation) * vsg::rotate(rotation) * vsg::scale(scale);
/// assumes matrix has no skew or perspective components
extern VSG_DECLSPEC bool decompose(const ldmat4& m, ldvec3& translation, ldquat& rotation, ldvec3& scale);

/// compute the bounding sphere that encloses a frustum defined by specified float ModelViewMatrixProjection
extern VSG_DECLSPEC sphere computeFrustumBound(const mat4& m);

Expand Down
17 changes: 9 additions & 8 deletions include/vsg/maths/vec2.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,15 @@ namespace vsg
explicit operator bool() const noexcept { return value[0] != 0.0 || value[1] != 0.0; }
};

using vec2 = t_vec2<float>; // float 2D vector
using dvec2 = t_vec2<double>; // double 2D vector
using bvec2 = t_vec2<int8_t>; // signed 8 bit integer 2D vector
using svec2 = t_vec2<int16_t>; // signed 16 bit integer 2D vector
using ivec2 = t_vec2<int32_t>; // signed 32 bit integer 2D vector
using ubvec2 = t_vec2<uint8_t>; // unsigned 8 bit integer 2D vector
using usvec2 = t_vec2<uint16_t>; // unsigned 16 bit integer 2D vector
using uivec2 = t_vec2<uint32_t>; // unsigned 32 bit integer 2D vector
using vec2 = t_vec2<float>; // float 2D vector
using dvec2 = t_vec2<double>; // double 2D vector
using ldvec2 = t_vec2<long double>; // long double 2D vector
using bvec2 = t_vec2<int8_t>; // signed 8 bit integer 2D vector
using svec2 = t_vec2<int16_t>; // signed 16 bit integer 2D vector
using ivec2 = t_vec2<int32_t>; // signed 32 bit integer 2D vector
using ubvec2 = t_vec2<uint8_t>; // unsigned 8 bit integer 2D vector
using usvec2 = t_vec2<uint16_t>; // unsigned 16 bit integer 2D vector
using uivec2 = t_vec2<uint32_t>; // unsigned 32 bit integer 2D vector

VSG_type_name(vsg::vec2);
VSG_type_name(vsg::dvec2);
Expand Down
Loading

0 comments on commit a7a7db7

Please sign in to comment.