7#include <simplesquirrel/simplesquirrel.hpp>
15uint32_t mixGrammar(uint32_t
value) {
21float grammarUnit(uint32_t
seed) {
return float(mixGrammar(
seed) >> 8u) * (1.f / 16777216.f); }
25 for (
size_t i = 1; i <
points.points().
size(); ++i) {
26 const auto&
a =
points.points()[i - 1];
27 const auto&
b =
points.points()[i];
28 const float dx =
b.x -
a.x;
29 const float dy =
b.y -
a.y;
30 const float dz =
b.z -
a.z;
38 for (
size_t i = 1; i <
points.points().
size(); ++i) {
39 const auto&
a =
points.points()[i - 1];
40 const auto&
b =
points.points()[i];
41 const float dx =
b.x -
a.x;
42 const float dy =
b.y -
a.y;
43 const float dz =
b.z -
a.z;
45 if (
length <= 0.f)
continue;
51 output.yaw = std::atan2(
dz,
dx) * 57.29577951308232f;
66 lastUsedLength_ = 0.f;
67 lastSplineLength_ = 0.f;
71 if (symbol.empty() || asset.empty() ||
length <= 0.f ||
weight <= 0.f)
return false;
72 const auto existing = modules_.find(symbol);
73 if (existing != modules_.end() && !existing->second.empty() &&
74 std::abs(existing->second.front().length -
length) > 0.0001f)
76 if (modules_.find(symbol) == modules_.end()) moduleOrder_.push_back(symbol);
82 if (modules_.erase(symbol) == 0)
return false;
83 moduleOrder_.erase(std::remove(moduleOrder_.begin(), moduleOrder_.end(), symbol), moduleOrder_.end());
89 return index >= 0 &&
index < int(moduleOrder_.size()) ? moduleOrder_[size_t(
index)] : std::string();
92 const auto found = modules_.find(symbol);
93 return found == modules_.end() ? 0 : int(
found->second.size());
96 const auto found = modules_.find(symbol);
102 const auto found = modules_.find(symbol);
110 Parser parser{grammar};
111 const auto parsed = parser.sequence();
112 if (!parser.error.empty()) {
113 error_ = parser.error;
116 if (parsed.empty()) {
117 error_ =
"grammar is empty";
120 std::vector<const Element*> stack;
121 stack.reserve(parsed.size());
123 while (!stack.empty()) {
124 const Element*
element = stack.back();
127 error_ =
"unknown module: " +
element->symbol;
130 for (
const auto& child :
element->children) stack.push_back(&child);
136 bool acceptIncomplete) {
138 lastSymbolCount_ = 0;
139 lastUsedLength_ = 0.f;
140 lastSplineLength_ = 0.f;
141 if (!controlPoints || controlPoints->
getCount() < 2) {
142 error_ =
"generate: requires at least two control points";
145 if (!
validate(grammar))
return nullptr;
146 Parser parser{grammar};
147 const auto parsed = parser.sequence();
148 lastSplineLength_ = polylineLength(*controlPoints);
149 if (!acceptIncomplete && sequenceMinLength(parsed) > lastSplineLength_ + 0.0001f) {
150 error_ =
"mandatory grammar does not fit spline";
153 std::vector<std::string> symbols;
155 if (!expandSequence(parsed, lastSplineLength_, symbols,
used)) {
156 error_ =
"grammar expansion failed";
162 for (
size_t i = 0; i < symbols.size(); ++i) {
163 const auto*
variant = chooseVariant(symbols[i], mixGrammar(
seed ^ uint32_t(i)));
165 if (!acceptIncomplete) {
166 error_ =
"expanded module does not fit spline: " + symbols[i];
174 const int pointIndex =
output.appendPoint(std::move(
point));
175 output.trySetStringAttribute(pointIndex,
"module", symbols[i]).expect(
"shape grammar module metadata schema");
176 output.trySetStringAttribute(pointIndex,
"asset",
variant->asset).expect(
"shape grammar asset metadata schema");
177 output.trySetFloatAttribute(pointIndex,
"length",
variant->length)
178 .expect(
"shape grammar length metadata schema");
181 lastSymbolCount_ =
output.getCount();
188 std::ostringstream
out;
189 out <<
"modules=" << modules_.size() <<
" symbols=" << lastSymbolCount_ <<
" used=" << lastUsedLength_
190 <<
" spline=" << lastSplineLength_;
191 if (!error_.empty())
out <<
" error=" << error_;
195void ShapeGrammar::Parser::skipWhitespace() {
199std::vector<ShapeGrammar::Element> ShapeGrammar::Parser::sequence(
char terminator) {
200 std::vector<Element>
result;
204 if (terminator !=
'\0' &&
text[
position] == terminator) {
212 if (!
error.empty())
return {};
213 if (
element.children.empty()) {
214 error =
"empty grammar group";
220 const unsigned char ch =
static_cast<unsigned char>(
text[
position]);
224 if (!std::isalpha(ch) && ch !=
'_' && ch !=
'-' && ch !=
'.')
break;
228 error =
"expected module at character " + std::to_string(
position);
256 error =
"expected comma at character " + std::to_string(
position);
260 if (terminator !=
'\0')
error =
"unterminated grammar group";
264float ShapeGrammar::elementMinLength(
const Element&
element)
const {
268 if (
found != modules_.end() && !
found->second.empty()) {
278float ShapeGrammar::sequenceMinLength(
const std::vector<Element>&
sequence,
size_t from)
const {
284bool ShapeGrammar::expandSequence(
const std::vector<Element>&
sequence,
float available,
285 std::vector<std::string>& symbols,
float&
used)
const {
286 for (
size_t i = 0; i <
sequence.size(); ++i) {
288 float unitLength = !
element.symbol.empty() ? elementMinLength(Element{
element.symbol, {}, 1, 1})
290 if (unitLength <= 0.f)
return false;
291 const float reservedAfter = sequenceMinLength(
sequence, i + 1);
292 int repeats =
element.repeatMin;
294 const float room = std::max(0.f, available -
used - reservedAfter);
295 repeats = std::max(
element.repeatMin,
int(std::floor(room / unitLength)));
299 for (
int repeat = 0; repeat < repeats; ++repeat) {
300 if (
used + unitLength + reservedAfter > available + 0.0001f)
return true;
302 symbols.push_back(
element.symbol);
305 float groupUsed = 0.f;
306 if (!expandSequence(
element.children, available -
used - reservedAfter, symbols, groupUsed))
315const ShapeModuleVariant* ShapeGrammar::chooseVariant(
const std::string& symbol, uint32_t
seed)
const {
316 const auto found = modules_.find(symbol);
317 if (
found == modules_.end() ||
found->second.empty())
return nullptr;
320 float choice = grammarUnit(
seed) * total;
323 if (choice <= 0.f)
return &
variant;
325 return &
found->second.back();
335 "ProcgenShapeGrammar", std::function<ShapeGrammar*()>([]() ->
ShapeGrammar* {
return nullptr; }),
true);
std::array< float, 3 > position
std::unique_ptr< gpgpu::Sequence > sequence
std::shared_ptr< const std::vector< glm::vec2 > > points
Script-friendly collection of attributed 3D samples.
int getCount() const
Returns the count.
Module-based shape grammar expanded continuously along a 3D polyline.
std::string getModuleSymbol(int index) const
Returns the module symbol.
bool removeModule(const std::string &symbol)
Removes module.
void clear()
Remove every registered symbol and diagnostic.
int getModuleCount() const
Returns the module count.
float getVariantLength(const std::string &symbol, int index) const
Returns the variant length.
std::string getError() const
Returns the error.
bool validate(const std::string &grammar)
Parse and validate grammar without generating output.
bool hasModule(const std::string &symbol) const
True when module.
int getVariantCount(const std::string &symbol) const
Returns the variant count.
bool addModule(const std::string &symbol, const std::string &asset, float length, float weight=1.f)
Register a weighted asset variant for a symbol.
std::string getVariantAsset(const std::string &symbol, int index) const
Returns the variant asset.
PointSet * generate(const std::string &grammar, PointSet *controlPoints, uint32_t seed, bool acceptIncomplete)
Expand grammar along control points.
std::string debugReport() const
Debug report.
double polylineLength(const Polyline &pl)
Total length of an open polyline.
void exposeShapeGrammar(ssq::Table &table)
Register shape-grammar script types on the Procgen module table.
One deterministic sample used by script-first procedural pipelines.