Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TSdlQuaternion.h
Go to the documentation of this file.
1#pragma once
2
5#include "Math/TQuaternion.h"
6#include "SDL/sdl_helpers.h"
7
8#include <Common/assertion.h>
9#include <Common/primitive_type.h>
10
11#include <type_traits>
12#include <string>
13
14namespace ph
15{
16
17template<typename Owner, typename Element = real, typename SdlValueType = TSdlValue<math::TQuaternion<Element>, Owner>>
18class TSdlQuaternion : public SdlValueType
19{
20 static_assert(std::is_base_of_v<TSdlAbstractValue<math::TQuaternion<Element>, Owner>, SdlValueType>,
21 "SdlValueType should be a subclass of TSdlAbstractValue.");
22
23 static_assert(std::is_same_v<Element, real>,
24 "Currently supports only ph::real");
25
26public:
27 template<typename ValueType>
28 TSdlQuaternion(std::string valueName, ValueType Owner::* const valuePtr) :
29 SdlValueType("quaternion", std::move(valueName), valuePtr)
30 {
31 if constexpr(std::is_same_v<SdlValueType, TSdlValue<math::TQuaternion<Element>, Owner>>)
32 {
34 }
35 }
36
37 std::string valueAsString(const math::TQuaternion<Element>& quat) const override
38 {
39 return quat.toString();
40 }
41
42 SdlNativeData ownedNativeData(Owner& owner) const override
43 {
44 math::TQuaternion<Element>* const quat = this->getValue(owner);
45 if constexpr(std::is_base_of_v<TSdlOptionalValue<math::TQuaternion<Element>, Owner>, SdlValueType>)
46 {
47 auto data = SdlNativeData(
48 [&optQuat = this->valueRef(owner)](std::size_t elementIdx) -> SdlGetterVariant
49 {
50 return optQuat
51 ? SdlNativeData::permissiveElementGetter(&((*optQuat)[elementIdx]))
52 : std::monostate{};
53 },
54 [&optQuat = this->valueRef(owner)](std::size_t elementIdx, SdlSetterVariant input) -> bool
55 {
56 if(input.isEmpty())
57 {
58 optQuat = std::nullopt;
59 return true;
60 }
61 else
62 {
63 if(!optQuat)
64 {
66 }
67
68 return SdlNativeData::permissiveElementSetter(input, &((*optQuat)[elementIdx]));
69 }
70 },
71 AnyNonConstPtr(quat));
72
74 data.elementType = sdl::number_type_of<Element>();
75 data.numElements = quat ? 4 : 0;
76 data.tupleSize = 4;
77 data.isNullClearable = true;
78 return data;
79 }
80 else
81 {
82 SdlNativeData data;
83 if(quat)
84 {
85 data = SdlNativeData(
86 [quat](std::size_t elementIdx) -> SdlGetterVariant
87 {
88 return SdlNativeData::permissiveElementGetter(&((*quat)[elementIdx]));
89 },
90 [quat](std::size_t elementIdx, SdlSetterVariant input) -> bool
91 {
92 return SdlNativeData::permissiveElementSetter(input, &((*quat)[elementIdx]));
93 },
94 AnyNonConstPtr(quat));
95 }
98 data.numElements = quat ? 4 : 0;
99 data.tupleSize = 4;
100 return data;
101 }
102 }
103
104protected:
106 Owner& owner,
107 const SdlInputClause& clause,
108 const SdlInputContext& ctx) const override
109 {
110 this->setValue(owner, sdl::load_quaternion<Element>(clause.value));
111 }
112
114 const Owner& owner,
115 SdlOutputClause& out_clause,
116 const SdlOutputContext& ctx) const override
117 {
118 if(const math::TQuaternion<Element>* quat = this->getConstValue(owner); quat)
119 {
120 sdl::save_quaternion(*quat, out_clause.value);
121 }
122 else
123 {
124 out_clause.isEmpty = true;
125 }
126 }
127};
128
129template<typename Owner, typename Element = real>
131
132}// end namespace ph
Carries SDL representation of various data during the input process. Helps to read input data such as...
Definition SdlInputClause.h:15
std::string value
Loaded stringified data of a clause. All potential SDL value prefixes or suffixes (e....
Definition SdlInputClause.h:25
Data that SDL input process can rely on.
Definition SdlInputContext.h:19
Definition SdlNativeData.h:88
static auto permissiveElementSetter(SdlSetterVariant input, ElementType *out_elementPtr) -> bool
Given a valid target element, set its value in a permissive way (with auto conversions).
Definition SdlNativeData.ipp:349
std::size_t numElements
Hint for number of elements in this block of native data. For example, numElements would be 12 for an...
Definition SdlNativeData.h:99
std::size_t tupleSize
Hint for number of elements that form a natural group. For an array of 10 vec3s, tupleSize may have a...
Definition SdlNativeData.h:105
static auto permissiveElementGetter(ElementType *elementPtr) -> SdlGetterVariant
Given a valid target element, get its value in a permissive way (with auto conversions).
Definition SdlNativeData.ipp:312
ESdlDataType elementType
Hint for the type of elements.
Definition SdlNativeData.h:113
ESdlDataFormat elementContainer
Hint for the type that encapsulates elements.
Definition SdlNativeData.h:109
Carries SDL representation of various data during the output process. Helps to write output data such...
Definition SdlOutputClause.h:14
std::string value
Stores stringified data of a clause. As the output clause generator knows best how its data look like...
Definition SdlOutputClause.h:29
bool isEmpty
If the clause carries no data and does not need to be written.
Definition SdlOutputClause.h:41
Data that SDL output process can rely on.
Definition SdlOutputContext.h:19
Definition SdlNativeData.h:24
Definition TSdlQuaternion.h:19
void loadFromSdl(Owner &owner, const SdlInputClause &clause, const SdlInputContext &ctx) const override
Load SDL value to actual value and store it in the owner's field. Implementations are highly encourag...
Definition TSdlQuaternion.h:105
TSdlQuaternion(std::string valueName, ValueType Owner::*const valuePtr)
Definition TSdlQuaternion.h:28
SdlNativeData ownedNativeData(Owner &owner) const override
Direct access to the field memory of an owner. Short-lived owner objects such as function parameter s...
Definition TSdlQuaternion.h:42
std::string valueAsString(const math::TQuaternion< Element > &quat) const override
Definition TSdlQuaternion.h:37
void saveToSdl(const Owner &owner, SdlOutputClause &out_clause, const SdlOutputContext &ctx) const override
Convert actual value back to SDL value. Saving a loaded value as SDL value should rarely fail–as load...
Definition TSdlQuaternion.h:113
void setValue(Owner &owner, T value) const override
Store a value.
Definition TSdlValue.ipp:38
const T * getConstValue(const Owner &owner) const override
Get a pointer to the stored value.
Definition TSdlValue.ipp:50
T * getValue(Owner &owner) const override
Get a pointer to the stored value.
Definition TSdlValue.ipp:44
TSdlValue & defaultTo(T defaultValue)
Definition TSdlValue.ipp:71
T & valueRef(Owner &owner) const
Definition TSdlValue.ipp:132
std::string toString() const
Definition TArithmeticArrayBase.ipp:825
Represents a quaternion.
Definition TQuaternion.h:17
void save_quaternion(const math::TQuaternion< Element > &value, std::string &out_str)
Definition sdl_helpers.ipp:353
math::TQuaternion< Element > load_quaternion(std::string_view sdlQuatStr)
Definition sdl_helpers.ipp:160
constexpr ESdlDataType number_type_of()
Definition sdl_helpers.ipp:478
The root for all renderer implementations.
Definition EEngineProject.h:6
TAnyPtr< false > AnyNonConstPtr
A type-safe, lightweight wrapper for any non-const raw pointer type.
Definition TAnyPtr.h:47
Definition TAABB2D.h:96
Low-level helpers for SDL. Helpers are in an additional sdl namespace.