2011-09-28 01:47:50 +04:00
|
|
|
// csg test core, used by throwntegether test and opencsg test
|
|
|
|
#include "csgtestcore.h"
|
|
|
|
|
2011-11-05 22:38:03 +04:00
|
|
|
#include "tests-common.h"
|
2011-10-05 01:24:32 +04:00
|
|
|
#include "system-gl.h"
|
2011-09-28 01:47:50 +04:00
|
|
|
#include "openscad.h"
|
2011-12-25 01:02:37 +04:00
|
|
|
#include "parsersettings.h"
|
2011-09-28 01:47:50 +04:00
|
|
|
#include "builtin.h"
|
2013-04-09 08:28:16 +04:00
|
|
|
#include "modcontext.h"
|
2011-09-28 01:47:50 +04:00
|
|
|
#include "node.h"
|
|
|
|
#include "module.h"
|
|
|
|
#include "polyset.h"
|
|
|
|
#include "Tree.h"
|
|
|
|
#include "CSGTermEvaluator.h"
|
|
|
|
#include "CGALEvaluator.h"
|
|
|
|
#include "PolySetCGALEvaluator.h"
|
|
|
|
|
2011-11-27 07:35:53 +04:00
|
|
|
#include <opencsg.h>
|
2011-09-28 01:47:50 +04:00
|
|
|
#include "OpenCSGRenderer.h"
|
|
|
|
#include "ThrownTogetherRenderer.h"
|
|
|
|
|
|
|
|
#include "csgterm.h"
|
2011-12-26 22:15:51 +04:00
|
|
|
#include "csgtermnormalizer.h"
|
2011-09-28 01:47:50 +04:00
|
|
|
#include "OffscreenView.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
2011-11-13 20:39:34 +04:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <boost/program_options.hpp>
|
2011-12-25 03:22:03 +04:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
2013-02-25 05:04:50 +04:00
|
|
|
#include "CsgInfo.h"
|
|
|
|
|
2011-11-13 20:39:34 +04:00
|
|
|
namespace po = boost::program_options;
|
2011-12-25 03:22:03 +04:00
|
|
|
namespace fs = boost::filesystem;
|
2012-01-14 08:02:15 +04:00
|
|
|
#include "boosty.h"
|
2011-09-28 01:47:50 +04:00
|
|
|
|
2011-11-13 20:39:34 +04:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2011-09-28 01:47:50 +04:00
|
|
|
using std::cerr;
|
|
|
|
using std::cout;
|
|
|
|
|
|
|
|
std::string commandline_commands;
|
|
|
|
|
|
|
|
//#define DEBUG
|
|
|
|
|
2011-11-13 20:39:34 +04:00
|
|
|
string info_dump(OffscreenView *glview)
|
|
|
|
{
|
2011-11-16 15:58:07 +04:00
|
|
|
assert(glview);
|
|
|
|
|
2011-11-14 00:34:01 +04:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#define compiler_info "GCC " << __VERSION__
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
#define compiler_info "MSVC " << _MSC_FULL_VER
|
|
|
|
#else
|
|
|
|
#define compiler_info "unknown compiler"
|
2011-12-01 08:24:14 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef OPENCSG_VERSION_STRING
|
|
|
|
#define OPENCSG_VERSION_STRING "unknown, <1.3.2"
|
2011-11-14 00:34:01 +04:00
|
|
|
#endif
|
2011-11-16 15:58:07 +04:00
|
|
|
|
2011-11-13 20:39:34 +04:00
|
|
|
std::stringstream out;
|
2011-11-16 15:58:07 +04:00
|
|
|
#define STRINGIFY(x) #x
|
|
|
|
#define TOSTRING(x) STRINGIFY(x)
|
2011-11-17 07:36:58 +04:00
|
|
|
out << "\nOpenSCAD Version: " << TOSTRING(OPENSCAD_VERSION)
|
2011-11-16 15:58:07 +04:00
|
|
|
<< "\nCompiled by: " << compiler_info
|
2011-11-17 07:36:58 +04:00
|
|
|
<< "\nCompile date: " << __DATE__
|
2011-11-16 15:58:07 +04:00
|
|
|
<< "\nBoost version: " << BOOST_LIB_VERSION
|
|
|
|
<< "\nEigen version: " << EIGEN_WORLD_VERSION << "."
|
|
|
|
<< EIGEN_MAJOR_VERSION << "." << EIGEN_MINOR_VERSION
|
2011-11-26 21:24:38 +04:00
|
|
|
<< "\nCGAL version: " << TOSTRING(CGAL_VERSION)
|
2011-12-01 08:24:14 +04:00
|
|
|
<< "\nOpenCSG version: " << OPENCSG_VERSION_STRING
|
2013-02-21 05:56:30 +04:00
|
|
|
<< "\n" << glview->getRendererInfo()
|
2011-11-16 15:58:07 +04:00
|
|
|
<< "\n";
|
|
|
|
|
2011-11-13 20:39:34 +04:00
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
po::variables_map parse_options(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
po::options_description desc("Allowed options");
|
|
|
|
desc.add_options()
|
2011-11-14 00:34:01 +04:00
|
|
|
("help,h", "help message")//;
|
|
|
|
("info,i", "information on GLEW, OpenGL, OpenSCAD, and OS")//;
|
2011-11-13 20:39:34 +04:00
|
|
|
|
2011-11-14 00:34:01 +04:00
|
|
|
// po::options_description hidden("Hidden options");
|
|
|
|
// hidden.add_options()
|
|
|
|
("input-file", po::value< vector<string> >(), "input file")
|
2012-01-09 06:20:27 +04:00
|
|
|
("output-file", po::value< vector<string> >(), "output file");
|
2011-11-13 20:39:34 +04:00
|
|
|
|
|
|
|
po::positional_options_description p;
|
2011-11-14 00:34:01 +04:00
|
|
|
p.add("input-file", 1).add("output-file", 1);
|
2011-11-13 20:39:34 +04:00
|
|
|
|
|
|
|
po::options_description all_options;
|
2011-11-14 00:34:01 +04:00
|
|
|
all_options.add(desc); // .add(hidden);
|
2011-11-13 20:39:34 +04:00
|
|
|
|
|
|
|
po::variables_map vm;
|
|
|
|
po::store(po::command_line_parser(argc, argv).options(all_options).positional(p).run(), vm);
|
2011-11-14 00:34:01 +04:00
|
|
|
po::notify(vm);
|
2011-11-13 20:39:34 +04:00
|
|
|
|
|
|
|
return vm;
|
|
|
|
}
|
|
|
|
|
2011-09-28 01:47:50 +04:00
|
|
|
int csgtestcore(int argc, char *argv[], test_type_e test_type)
|
|
|
|
{
|
2011-11-13 20:39:34 +04:00
|
|
|
bool sysinfo_dump = false;
|
|
|
|
const char *filename, *outfilename = NULL;
|
2011-11-14 00:34:01 +04:00
|
|
|
po::variables_map vm;
|
|
|
|
try {
|
|
|
|
vm = parse_options(argc, argv);
|
|
|
|
} catch ( po::error e ) {
|
|
|
|
cerr << "error parsing options\n";
|
|
|
|
}
|
2011-11-13 20:39:34 +04:00
|
|
|
if (vm.count("info")) sysinfo_dump = true;
|
2011-11-14 00:34:01 +04:00
|
|
|
if (vm.count("input-file"))
|
2011-11-13 20:39:34 +04:00
|
|
|
filename = vm["input-file"].as< vector<string> >().begin()->c_str();
|
2011-11-14 00:34:01 +04:00
|
|
|
if (vm.count("output-file"))
|
2011-11-13 20:39:34 +04:00
|
|
|
outfilename = vm["output-file"].as< vector<string> >().begin()->c_str();
|
2011-09-28 01:47:50 +04:00
|
|
|
|
2011-11-14 00:34:01 +04:00
|
|
|
if ((!filename || !outfilename) && !sysinfo_dump) {
|
2011-11-13 20:39:34 +04:00
|
|
|
cerr << "Usage: " << argv[0] << " <file.scad> <output.png>\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-09-28 01:47:50 +04:00
|
|
|
|
2011-11-06 21:37:12 +04:00
|
|
|
Builtins::instance()->initialize();
|
2011-09-28 01:47:50 +04:00
|
|
|
|
2011-12-25 03:22:03 +04:00
|
|
|
fs::path original_path = fs::current_path();
|
2011-09-28 01:47:50 +04:00
|
|
|
|
2012-01-14 08:02:15 +04:00
|
|
|
std::string currentdir = boosty::stringy( fs::current_path() );
|
2011-09-28 01:47:50 +04:00
|
|
|
|
2013-03-12 09:14:58 +04:00
|
|
|
parser_init(boosty::stringy(fs::path(argv[0]).branch_path()));
|
|
|
|
add_librarydir(boosty::stringy(fs::path(argv[0]).branch_path() / "../libraries"));
|
2011-09-28 01:47:50 +04:00
|
|
|
|
2013-04-24 17:17:25 +04:00
|
|
|
ModuleContext top_ctx;
|
|
|
|
top_ctx.registerBuiltin();
|
2011-09-28 01:47:50 +04:00
|
|
|
|
2013-04-24 17:17:25 +04:00
|
|
|
FileModule *root_module;
|
2013-04-09 08:28:16 +04:00
|
|
|
ModuleInstantiation root_inst("group");
|
2011-09-28 01:47:50 +04:00
|
|
|
|
2011-11-14 00:34:01 +04:00
|
|
|
if (sysinfo_dump)
|
|
|
|
root_module = parse("sphere();","",false);
|
|
|
|
else
|
|
|
|
root_module = parsefile(filename);
|
|
|
|
|
2011-11-05 22:38:03 +04:00
|
|
|
if (!root_module) {
|
2011-09-28 01:47:50 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:36:27 +04:00
|
|
|
if (!sysinfo_dump) {
|
2013-05-09 14:12:58 +04:00
|
|
|
fs::path fpath = boosty::absolute(fs::path(filename));
|
|
|
|
fs::path fparent = fpath.parent_path();
|
|
|
|
fs::current_path(fparent);
|
|
|
|
top_ctx.setDocumentPath(fparent.string());
|
2011-12-22 16:36:27 +04:00
|
|
|
}
|
2011-09-28 01:47:50 +04:00
|
|
|
|
|
|
|
AbstractNode::resetIndexCounter();
|
2013-04-24 17:17:25 +04:00
|
|
|
AbstractNode *absolute_root_node = root_module->instantiate(&top_ctx, &root_inst);
|
2011-09-28 01:47:50 +04:00
|
|
|
AbstractNode *root_node;
|
|
|
|
// Do we have an explicit root node (! modifier)?
|
|
|
|
if (!(root_node = find_root_tag(absolute_root_node))) root_node = absolute_root_node;
|
|
|
|
|
|
|
|
Tree tree(root_node);
|
|
|
|
|
2013-03-03 19:22:36 +04:00
|
|
|
CsgInfo csgInfo = CsgInfo();
|
2013-03-04 05:47:07 +04:00
|
|
|
if ( !csgInfo.compile_chains( tree ) ) return 1;
|
2011-12-22 07:09:52 +04:00
|
|
|
|
2011-12-25 03:22:03 +04:00
|
|
|
fs::current_path(original_path);
|
2011-09-28 01:47:50 +04:00
|
|
|
|
2011-10-16 05:22:06 +04:00
|
|
|
try {
|
|
|
|
csgInfo.glview = new OffscreenView(512,512);
|
|
|
|
} catch (int error) {
|
|
|
|
fprintf(stderr,"Can't create OpenGL OffscreenView. Code: %i. Exiting.\n", error);
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-11-27 07:35:53 +04:00
|
|
|
|
2011-11-13 20:39:34 +04:00
|
|
|
if (sysinfo_dump) cout << info_dump(csgInfo.glview);
|
2013-03-03 21:41:37 +04:00
|
|
|
Camera camera(Camera::VECTOR);
|
|
|
|
camera.center << 0,0,0;
|
2012-01-29 22:01:15 +04:00
|
|
|
double radius = 1.0;
|
2011-09-28 01:47:50 +04:00
|
|
|
|
2012-01-29 22:01:15 +04:00
|
|
|
if (csgInfo.root_chain) {
|
|
|
|
BoundingBox bbox = csgInfo.root_chain->getBoundingBox();
|
2013-03-03 21:41:37 +04:00
|
|
|
camera.center = (bbox.min() + bbox.max()) / 2;
|
2012-01-29 22:01:15 +04:00
|
|
|
radius = (bbox.max() - bbox.min()).norm() / 2;
|
|
|
|
}
|
2011-09-28 01:47:50 +04:00
|
|
|
Vector3d cameradir(1, 1, -0.5);
|
2013-03-03 21:41:37 +04:00
|
|
|
camera.eye = camera.center - radius*1.8*cameradir;
|
|
|
|
csgInfo.glview->setCamera(camera);
|
2011-09-28 01:47:50 +04:00
|
|
|
|
|
|
|
OpenCSGRenderer opencsgRenderer(csgInfo.root_chain, csgInfo.highlights_chain, csgInfo.background_chain, csgInfo.glview->shaderinfo);
|
|
|
|
ThrownTogetherRenderer thrownTogetherRenderer(csgInfo.root_chain, csgInfo.highlights_chain, csgInfo.background_chain);
|
|
|
|
|
|
|
|
if (test_type == TEST_THROWNTOGETHER)
|
|
|
|
csgInfo.glview->setRenderer(&thrownTogetherRenderer);
|
|
|
|
else
|
|
|
|
csgInfo.glview->setRenderer(&opencsgRenderer);
|
|
|
|
|
2011-11-27 07:35:53 +04:00
|
|
|
OpenCSG::setContext(0);
|
|
|
|
OpenCSG::setOption(OpenCSG::OffscreenSetting, OpenCSG::FrameBufferObject);
|
2011-09-28 01:47:50 +04:00
|
|
|
|
2011-11-27 07:35:53 +04:00
|
|
|
csgInfo.glview->paintGL();
|
|
|
|
|
2013-01-30 06:04:03 +04:00
|
|
|
if (outfilename) csgInfo.glview->save(outfilename);
|
2011-09-28 01:47:50 +04:00
|
|
|
|
2011-11-27 08:20:18 +04:00
|
|
|
delete root_node;
|
|
|
|
delete root_module;
|
|
|
|
|
2011-11-06 21:37:12 +04:00
|
|
|
Builtins::instance(true);
|
2011-09-28 01:47:50 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|