-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shape and volume builder (#1126)
* Add 'inserted' result to CSG tree * Return insertion from CSG unit builder * Fix test JSON output for CSG tree * Add script for generating graphs from CSG tree * Add shape, object, and sketch volume builder * Add shape test and volume construction * Rename {Shape, ShapeImpl} -> {ShapeBase, Shape} and add constructor
- Loading branch information
Showing
21 changed files
with
707 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright 2024 UT-Battelle, LLC, and other Celeritas developers. | ||
# See the top-level COPYRIGHT file for details. | ||
# SPDX-License-Identifier: (Apache-2.0 OR MIT) | ||
""" | ||
Convert an ORANGE CSG JSON representation to a GraphViz input. | ||
""" | ||
import json | ||
|
||
class DoxygenGenerator: | ||
def __init__(self, f): | ||
self.f = f | ||
self.write = f.write | ||
self.write("""\ | ||
strict digraph { | ||
rankdir=TB; | ||
""") | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, type, value, traceback): | ||
self.write("}\n") | ||
|
||
def write_literal(self, i, value): | ||
self.write(f"{i:02d} [label=\"{value}\"];\n") | ||
|
||
def write_node(self, i, value, edges): | ||
self.write_literal(i, f"{i}: {value}") | ||
for e in edges: | ||
self.write(f"{i:02d} -> {e:02d};\n") | ||
|
||
def process(gen, tree): | ||
for (i, node) in enumerate(tree): | ||
if isinstance(node, str): | ||
# True (or false??) | ||
gen.write_literal(i, node) | ||
continue | ||
(nodetype, value) = node | ||
if nodetype == "S": | ||
# Surface | ||
gen.write_literal(i, f"S{value}") | ||
elif nodetype in ("~", "="): | ||
# Negated | ||
gen.write_node(i, nodetype, [value]) | ||
else: | ||
# Joined | ||
gen.write_node(i, nodetype, value) | ||
|
||
def run(infile, outfile): | ||
tree = json.load(infile) | ||
with DoxygenGenerator(outfile) as gen: | ||
process(gen, tree) | ||
|
||
def main(): | ||
import argparse | ||
import sys | ||
|
||
parser = argparse.ArgumentParser(description=__doc__) | ||
parser.add_argument( | ||
"input", | ||
help="Input filename (- for stdin)") | ||
parser.add_argument( | ||
"-o", "--output", | ||
default=None, | ||
help="Output filename (empty for stdout)") | ||
args = parser.parse_args() | ||
|
||
if args.input == "-": | ||
infile = sys.stdin | ||
else: | ||
infile = open(args.input) | ||
|
||
if not args.output: | ||
outfile = sys.stdout | ||
else: | ||
outfile = open(args.output) | ||
|
||
run(infile, outfile) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//----------------------------------*-C++-*----------------------------------// | ||
// Copyright 2024 UT-Battelle, LLC, and other Celeritas developers. | ||
// See the top-level COPYRIGHT file for details. | ||
// SPDX-License-Identifier: (Apache-2.0 OR MIT) | ||
//---------------------------------------------------------------------------// | ||
//! \file orange/orangeinp/ObjectInterface.hh | ||
//---------------------------------------------------------------------------// | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <string_view> | ||
|
||
#include "corecel/Macros.hh" | ||
|
||
#include "CsgTypes.hh" | ||
|
||
namespace celeritas | ||
{ | ||
namespace orangeinp | ||
{ | ||
namespace detail | ||
{ | ||
class VolumeBuilder; | ||
} | ||
|
||
//---------------------------------------------------------------------------// | ||
/*! | ||
* Base class for constructing high-level CSG objects in ORANGE. | ||
*/ | ||
class ObjectInterface | ||
{ | ||
public: | ||
//!@{ | ||
//! \name Type aliases | ||
using SPConstObject = std::shared_ptr<ObjectInterface const>; | ||
using VolumeBuilder = detail::VolumeBuilder; | ||
//!@} | ||
|
||
public: | ||
//! Short unique name of this object | ||
virtual std::string_view label() const = 0; | ||
|
||
//! Construct a volume from this object | ||
virtual NodeId build(VolumeBuilder&) const = 0; | ||
|
||
protected: | ||
//!@{ | ||
//! Allow construction and assignment only through daughter classes | ||
ObjectInterface() = default; | ||
virtual ~ObjectInterface() = default; | ||
CELER_DEFAULT_COPY_MOVE(ObjectInterface); | ||
//!@} | ||
}; | ||
|
||
//---------------------------------------------------------------------------// | ||
} // namespace orangeinp | ||
} // namespace celeritas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//----------------------------------*-C++-*----------------------------------// | ||
// Copyright 2024 UT-Battelle, LLC, and other Celeritas developers. | ||
// See the top-level COPYRIGHT file for details. | ||
// SPDX-License-Identifier: (Apache-2.0 OR MIT) | ||
//---------------------------------------------------------------------------// | ||
//! \file orange/orangeinp/Shape.cc | ||
//---------------------------------------------------------------------------// | ||
#include "Shape.hh" | ||
|
||
#include "ConvexSurfaceBuilder.hh" | ||
#include "CsgTreeUtils.hh" | ||
|
||
#include "detail/ConvexSurfaceState.hh" | ||
#include "detail/CsgUnitBuilder.hh" | ||
#include "detail/VolumeBuilder.hh" | ||
|
||
namespace celeritas | ||
{ | ||
namespace orangeinp | ||
{ | ||
//---------------------------------------------------------------------------// | ||
/*! | ||
* Construct a volume from this shape. | ||
*/ | ||
NodeId ShapeBase::build(VolumeBuilder& vb) const | ||
{ | ||
// Set input attributes for surface state | ||
detail::ConvexSurfaceState css; | ||
css.transform = &vb.local_transform(); | ||
css.object_name = this->label(); | ||
css.make_face_name = {}; // No prefix for standalone shapes | ||
|
||
// Construct surfaces | ||
auto sb = ConvexSurfaceBuilder(&vb.unit_builder(), &css); | ||
this->build_interior(sb); | ||
|
||
// Intersect the given surfaces to create a new CSG node | ||
auto node_id = vb.insert_region(Label{std::move(css.object_name)}, | ||
Joined{op_and, std::move(css.nodes)}, | ||
calc_merged_bzone(css)); | ||
|
||
return node_id; | ||
} | ||
|
||
//---------------------------------------------------------------------------// | ||
// EXPLICIT INSTANTIATION | ||
//---------------------------------------------------------------------------// | ||
|
||
template class Shape<Box>; | ||
template class Shape<Cone>; | ||
template class Shape<Cylinder>; | ||
template class Shape<Ellipsoid>; | ||
template class Shape<Prism>; | ||
template class Shape<Sphere>; | ||
|
||
//---------------------------------------------------------------------------// | ||
} // namespace orangeinp | ||
} // namespace celeritas |
Oops, something went wrong.