RigsofRods
Soft-body Physics Simulation
RigDef_SequentialImporter.h
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2013-2020 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
25 
26 #pragma once
27 
28 #include "RigDef_Prerequisites.h"
29 #include "RigDef_File.h"
30 
31 #include <memory>
32 #include <vector>
33 
34 namespace RigDef
35 {
36 
37 /*
38  @brief Legacy rig-file importer.
39 
40  RoR's physics work on this principle:
41  1. There is a node array, pre-allocated to some 5000 items.
42  2. As .truck file is parsed, defined nodes are reserved in this array and accessible through their
43  index. Alternatively, nodes can be defined "named"; in that case, a <name->node index> translation
44  is performed and node can be addressed by both.
45  3. Nodes are also generated by some sections (cinecam, wheels...). These can also be adressed by index.
46 
47  Traditionally, the order of nodes in nodes-array was determined by order of definitions in .truck file.
48  This approach had downsides:
49  1. Nodes could be adressed before they were defined
50  This could be detected, but for compatibility reasons, parser emitted a warning only.
51  2. Non-existent nodes could be adressed.
52  Due to pre-allocated nature of node-array, this passed unnoticed.
53  It could be explicitly detected, but parsers weren't equipped to do so in every case.
54  3. Very hard to determine index of some nodes.
55  Creating rig was traditionally a manual process, and not every section supported named nodes (i.e. axles)
56  Knowing index of node was necessary, but with named/generated nodes, it was hard to tell.
57  3. Unfeasible for visual editor with mouse controls.
58  This approach would, by nature, re-order definitions and make direct index-adressing impossible.
59 
60  To ease code maintenance, enable bullet-proof checking and prepare grounds for visual rig editor,
61  a new approach was applied: Truckfile would be loaded to RigDef::Document structure in it's entirety,
62  and then parsed in pre-determined order.
63  Order (section name[number of nodes generated per line]):
64  1. nodes[1]
65  2. nodes2[1]
66  3. cinecam[1]
67  4. wheels[num_rays*2]
68  5. wheels2[num_rays*4]
69  6. meshwheels[num_rays*2]
70  7. meshwheels2[num_rays*2]
71  8. flexbodywheels[num_rays*4]
72 
73  Naturally, older truckfiles which rely on user-determined order of definitions needed to be converted
74  with index-references updated to match new node-array positions. This class does exactly that.
75 
76 */
78 {
79 public:
80  struct NodeMapEntry
81  {
83  {
88 
89  DETAIL_UNDEFINED = 0xFFFFFFFF
90  };
91 
94  origin_detail(detail),
96  node_id(id)
97  {}
98 
102  unsigned node_sub_index;
103  };
104 
105  struct Message // TODO: remove, use console API directly
106  {
107  enum Type
108  {
113 
114  TYPE_INVALID = 0xFFFFFFFF
115  };
116  };
117 
118  void Init(bool enabled);
119 
120  void Disable()
121  {
122  m_enabled = false;
123  m_all_nodes.clear();
124  }
125 
126  inline bool IsEnabled() const { return m_enabled; }
127 
128  bool AddNumberedNode(unsigned int number);
129 
130  bool AddNamedNode(std::string const & name);
131 
133 
134  void GenerateNodesForWheel(Keyword generated_from, int num_rays, bool has_rigidity_node);
135 
137  void Process(RigDef::DocumentPtr def);
138 
139  std::string GetNodeStatistics();
140  std::string IterateAndPrintAllNodes();
141 
142 private:
143 
144  void ProcessModule(std::shared_ptr<RigDef::Document::Module> module);
145 
146  Node::Ref ResolveNode(Node::Ref const & noderef_in);
147  Node::Ref ResolveNodeByIndex(unsigned int index, unsigned int def_line_number);
149  void ResolveNodeRanges(std::vector<Node::Range>& ranges);
150  void ResolveFlexbodyForset(std::vector<Node::Range>& ranges, std::vector<Node::Ref>& out_nodes);
151 
152  void AddMessage(Message::Type msg_type, std::string text);
153 
154  std::vector<NodeMapEntry> m_all_nodes;
155  std::map<std::string, NodeMapEntry> m_named_nodes;
164  bool m_enabled;
165 
166  // Logging
170  std::shared_ptr<Document::Module> m_current_module;
171 };
172 
173 } // namespace RigDef
RigDef::SequentialImporter::m_num_resolved_to_self
int m_num_resolved_to_self
Definition: RigDef_SequentialImporter.h:168
RigDef::SequentialImporter::m_current_keyword
Keyword m_current_keyword
Definition: RigDef_SequentialImporter.h:169
RigDef::Node::Ref
Legacy parser resolved references on-the-fly and the condition to check named nodes was "are there an...
Definition: RigDef_Node.h:77
RigDef::SequentialImporter::m_named_nodes
std::map< std::string, NodeMapEntry > m_named_nodes
Definition: RigDef_SequentialImporter.h:155
RigDef::SequentialImporter::m_num_meshwheels_nodes
unsigned m_num_meshwheels_nodes
Definition: RigDef_SequentialImporter.h:161
RigDef::SequentialImporter::NodeMapEntry::DETAIL_WHEEL_TYRE_A
@ DETAIL_WHEEL_TYRE_A
Definition: RigDef_SequentialImporter.h:84
RigDef::SequentialImporter::NodeMapEntry::DETAIL_WHEEL_RIM_B
@ DETAIL_WHEEL_RIM_B
Definition: RigDef_SequentialImporter.h:87
RigDef::SequentialImporter::NodeMapEntry::node_sub_index
unsigned node_sub_index
Definition: RigDef_SequentialImporter.h:102
RigDef::SequentialImporter::NodeMapEntry::DETAIL_WHEEL_TYRE_B
@ DETAIL_WHEEL_TYRE_B
Definition: RigDef_SequentialImporter.h:85
RigDef_Prerequisites.h
RigDef::SequentialImporter::NodeMapEntry::OriginDetail
OriginDetail
Definition: RigDef_SequentialImporter.h:82
RigDef::SequentialImporter::AddNumberedNode
bool AddNumberedNode(unsigned int number)
Definition: RigDef_SequentialImporter.cpp:721
RigDef::SequentialImporter::AddGeneratedNode
void AddGeneratedNode(Keyword generated_from, NodeMapEntry::OriginDetail detail=NodeMapEntry::DETAIL_UNDEFINED)
Definition: RigDef_SequentialImporter.cpp:751
RigDef::SequentialImporter::ResolveNodeRanges
void ResolveNodeRanges(std::vector< Node::Range > &ranges)
Definition: RigDef_SequentialImporter.cpp:319
RigDef::SequentialImporter::Message::Type
Type
Definition: RigDef_SequentialImporter.h:107
RigDef::SequentialImporter::Message::TYPE_INFO
@ TYPE_INFO
Definition: RigDef_SequentialImporter.h:109
RigDef::SequentialImporter::Message
Definition: RigDef_SequentialImporter.h:105
RigDef::SequentialImporter::NodeMapEntry::NodeMapEntry
NodeMapEntry(Keyword keyword, Node::Id id, unsigned node_sub_index, OriginDetail detail=NodeMapEntry::DETAIL_UNDEFINED)
Definition: RigDef_SequentialImporter.h:92
RigDef::SequentialImporter::m_num_cinecam_nodes
unsigned m_num_cinecam_nodes
Definition: RigDef_SequentialImporter.h:158
RigDef::SequentialImporter::ResolveNode
Node::Ref ResolveNode(Node::Ref const &noderef_in)
Definition: RigDef_SequentialImporter.cpp:185
RigDef::SequentialImporter::m_total_resolved
int m_total_resolved
Definition: RigDef_SequentialImporter.h:167
RigDef::SequentialImporter::m_num_wheels2_nodes
unsigned m_num_wheels2_nodes
Definition: RigDef_SequentialImporter.h:160
RigDef::SequentialImporter::Message::TYPE_ERROR
@ TYPE_ERROR
Definition: RigDef_SequentialImporter.h:111
RigDef::SequentialImporter::m_num_flexbodywheels_nodes
unsigned m_num_flexbodywheels_nodes
Definition: RigDef_SequentialImporter.h:163
RigDef::SequentialImporter::m_current_module
std::shared_ptr< Document::Module > m_current_module
Definition: RigDef_SequentialImporter.h:170
RigDef::SequentialImporter::ResolveNodeByIndex
Node::Ref ResolveNodeByIndex(unsigned int index, unsigned int def_line_number)
Definition: RigDef_SequentialImporter.cpp:156
RigDef::SequentialImporter::NodeMapEntry::origin_keyword
RigDef::Keyword origin_keyword
Definition: RigDef_SequentialImporter.h:99
keyword
static int keyword
Definition: Bench_TruckParser_IdentifyKeyword.cpp:1448
RigDef
Definition: RigDef_File.cpp:32
RigDef::Keyword
Keyword
Definition: RigDef_File.h:65
RigDef::SequentialImporter::GetNodeArrayOffset
unsigned GetNodeArrayOffset(Keyword keyword)
Definition: RigDef_SequentialImporter.cpp:136
RigDef::SequentialImporter::Message::TYPE_INVALID
@ TYPE_INVALID
Definition: RigDef_SequentialImporter.h:114
RigDef::SequentialImporter::IterateAndPrintAllNodes
std::string IterateAndPrintAllNodes()
Definition: RigDef_SequentialImporter.cpp:794
RigDef::SequentialImporter
Definition: RigDef_SequentialImporter.h:77
RigDef::SequentialImporter::m_num_wheels_nodes
unsigned m_num_wheels_nodes
Definition: RigDef_SequentialImporter.h:159
RigDef::SequentialImporter::m_all_nodes
std::vector< NodeMapEntry > m_all_nodes
Definition: RigDef_SequentialImporter.h:154
RigDef::SequentialImporter::NodeMapEntry::node_id
Node::Id node_id
Definition: RigDef_SequentialImporter.h:101
RigDef::SequentialImporter::GenerateNodesForWheel
void GenerateNodesForWheel(Keyword generated_from, int num_rays, bool has_rigidity_node)
Definition: RigDef_SequentialImporter.cpp:54
RigDef::SequentialImporter::m_num_meshwheels2_nodes
unsigned m_num_meshwheels2_nodes
Definition: RigDef_SequentialImporter.h:162
RigDef::SequentialImporter::NodeMapEntry
Definition: RigDef_SequentialImporter.h:80
RigDef::SequentialImporter::IsEnabled
bool IsEnabled() const
Definition: RigDef_SequentialImporter.h:126
RigDef::SequentialImporter::Process
void Process(RigDef::DocumentPtr def)
Traverse whole rig definition and resolve all node references.
Definition: RigDef_SequentialImporter.cpp:85
RigDef::SequentialImporter::Init
void Init(bool enabled)
Definition: RigDef_SequentialImporter.cpp:36
RigDef::SequentialImporter::NodeMapEntry::origin_detail
OriginDetail origin_detail
Definition: RigDef_SequentialImporter.h:100
RigDef::SequentialImporter::NodeMapEntry::DETAIL_UNDEFINED
@ DETAIL_UNDEFINED
Definition: RigDef_SequentialImporter.h:89
RigDef::SequentialImporter::GetNodeStatistics
std::string GetNodeStatistics()
Definition: RigDef_SequentialImporter.cpp:775
RigDef::SequentialImporter::ResolveFlexbodyForset
void ResolveFlexbodyForset(std::vector< Node::Range > &ranges, std::vector< Node::Ref > &out_nodes)
Definition: RigDef_SequentialImporter.cpp:248
RigDef::Node::Id
Abstract node ID (numbered or named) Node name is always available.
Definition: RigDef_Node.h:44
RigDef::SequentialImporter::AddMessage
void AddMessage(Message::Type msg_type, std::string text)
Definition: RigDef_SequentialImporter.cpp:106
RigDef::SequentialImporter::Message::TYPE_FATAL_ERROR
@ TYPE_FATAL_ERROR
Definition: RigDef_SequentialImporter.h:112
RigDef::SequentialImporter::m_enabled
bool m_enabled
Definition: RigDef_SequentialImporter.h:164
RigDef::SequentialImporter::AddNamedNode
bool AddNamedNode(std::string const &name)
Definition: RigDef_SequentialImporter.cpp:735
RigDef::SequentialImporter::ProcessModule
void ProcessModule(std::shared_ptr< RigDef::Document::Module > module)
Definition: RigDef_SequentialImporter.cpp:394
RigDef::SequentialImporter::Disable
void Disable()
Definition: RigDef_SequentialImporter.h:120
RigDef::SequentialImporter::NodeMapEntry::DETAIL_WHEEL_RIM_A
@ DETAIL_WHEEL_RIM_A
Definition: RigDef_SequentialImporter.h:86
RigDef::DocumentPtr
std::shared_ptr< Document > DocumentPtr
Definition: RigDef_Prerequisites.h:38
RigDef::SequentialImporter::Message::TYPE_WARNING
@ TYPE_WARNING
Definition: RigDef_SequentialImporter.h:110
RigDef::SequentialImporter::m_num_named_nodes
unsigned m_num_named_nodes
Definition: RigDef_SequentialImporter.h:157
RigDef::SequentialImporter::m_num_numbered_nodes
unsigned m_num_numbered_nodes
Definition: RigDef_SequentialImporter.h:156
RigDef_File.h
Data structures representing 'truck' file format, see https://docs.rigsofrods.org/vehicle-creation/fi...