2011-09-01 07:04:42 +04:00
|
|
|
/*
|
2011-11-05 22:01:19 +04:00
|
|
|
* OpenSCAD (www.openscad.org)
|
|
|
|
* Copyright (C) 2009-2011 Clifford Wolf <clifford@clifford.at> and
|
|
|
|
* Marius Kintel <marius@kintel.net>
|
2011-09-01 07:04:42 +04:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* As a special exception, you have permission to link this program
|
|
|
|
* with the CGAL library and distribute executables, as long as you
|
|
|
|
* follow the requirements of the GNU GPL in regard to all of the
|
|
|
|
* software in the executable aside from CGAL.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-11-05 22:38:03 +04:00
|
|
|
#include "tests-common.h"
|
2011-09-01 07:04:42 +04:00
|
|
|
#include "openscad.h"
|
2011-12-25 03:22:03 +04:00
|
|
|
#include "parsersettings.h"
|
2011-09-01 07:04:42 +04:00
|
|
|
#include "node.h"
|
|
|
|
#include "module.h"
|
|
|
|
#include "polyset.h"
|
2013-04-09 08:28:16 +04:00
|
|
|
#include "modcontext.h"
|
2011-09-01 07:04:42 +04:00
|
|
|
#include "value.h"
|
|
|
|
#include "export.h"
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "Tree.h"
|
|
|
|
#include "CGAL_Nef_polyhedron.h"
|
|
|
|
#include "CGALEvaluator.h"
|
|
|
|
#include "PolySetCGALEvaluator.h"
|
2011-09-04 03:59:06 +04:00
|
|
|
#include "CGALRenderer.h"
|
2011-09-01 19:02:50 +04:00
|
|
|
#include "CGAL_renderer.h"
|
|
|
|
#include "cgal.h"
|
2011-09-01 07:04:42 +04:00
|
|
|
#include "OffscreenView.h"
|
|
|
|
|
2011-10-11 03:01:51 +04:00
|
|
|
#ifndef _MSC_VER
|
2011-09-01 07:04:42 +04:00
|
|
|
#include <getopt.h>
|
2011-10-11 03:01:51 +04:00
|
|
|
#endif
|
2011-09-01 07:04:42 +04:00
|
|
|
#include <iostream>
|
|
|
|
#include <assert.h>
|
2011-09-04 02:03:16 +04:00
|
|
|
#include <sstream>
|
2011-09-01 07:04:42 +04:00
|
|
|
|
2011-12-25 03:22:03 +04:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
namespace fs = boost::filesystem;
|
2012-01-14 08:02:15 +04:00
|
|
|
#include "boosty.h"
|
2011-12-25 03:22:03 +04:00
|
|
|
|
2011-09-04 02:03:16 +04:00
|
|
|
std::string commandline_commands;
|
2011-12-25 03:22:03 +04:00
|
|
|
std::string currentdir;
|
2011-09-01 07:04:42 +04:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
void cgalTree(Tree &tree)
|
|
|
|
{
|
|
|
|
assert(tree.root());
|
|
|
|
|
2011-09-11 12:51:55 +04:00
|
|
|
CGALEvaluator evaluator(tree);
|
2011-09-01 07:04:42 +04:00
|
|
|
Traverser evaluate(evaluator, *tree.root(), Traverser::PRE_AND_POSTFIX);
|
|
|
|
evaluate.execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CsgInfo
|
|
|
|
{
|
|
|
|
OffscreenView *glview;
|
|
|
|
};
|
|
|
|
|
2011-10-16 00:48:41 +04:00
|
|
|
|
|
|
|
extern Vector3d getBoundingCenter(BoundingBox bbox);
|
|
|
|
extern double getBoundingRadius(BoundingBox bbox);
|
|
|
|
|
|
|
|
|
2011-09-01 07:04:42 +04:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2011-10-16 07:59:54 +04:00
|
|
|
if (argc != 3) {
|
|
|
|
fprintf(stderr, "Usage: %s <file.scad> <output.png>\n", argv[0]);
|
2011-09-01 07:04:42 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *filename = argv[1];
|
2011-10-16 07:59:54 +04:00
|
|
|
const char *outfile = argv[2];
|
2011-09-01 07:04:42 +04:00
|
|
|
|
2011-11-27 02:33:19 +04:00
|
|
|
#ifdef ENABLE_CGAL
|
|
|
|
// Causes CGAL errors to abort directly instead of throwing exceptions
|
|
|
|
// (which we don't catch). This gives us stack traces without rerunning in gdb.
|
|
|
|
CGAL::set_error_behaviour(CGAL::ABORT);
|
|
|
|
#endif
|
2011-11-06 21:37:12 +04:00
|
|
|
Builtins::instance()->initialize();
|
2011-09-01 07:04:42 +04:00
|
|
|
|
2011-12-25 03:22:03 +04:00
|
|
|
fs::path original_path = fs::current_path();
|
|
|
|
|
2012-01-14 08:02:15 +04:00
|
|
|
currentdir = boosty::stringy( fs::current_path() );
|
2011-09-01 07:04:42 +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-01 07:04:42 +04:00
|
|
|
|
2013-04-24 17:17:25 +04:00
|
|
|
ModuleContext top_ctx;
|
|
|
|
top_ctx.registerBuiltin();
|
2011-09-01 07:04:42 +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-01 07:04:42 +04:00
|
|
|
|
2011-11-05 22:38:03 +04:00
|
|
|
root_module = parsefile(filename);
|
|
|
|
if (!root_module) {
|
2011-09-01 07:04:42 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
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-09-01 07:04:42 +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-04 20:53:09 +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;
|
2011-09-01 07:04:42 +04:00
|
|
|
|
|
|
|
Tree tree(root_node);
|
|
|
|
|
|
|
|
CsgInfo csgInfo;
|
2011-09-11 12:51:55 +04:00
|
|
|
CGALEvaluator cgalevaluator(tree);
|
2011-09-01 07:04:42 +04:00
|
|
|
PolySetCGALEvaluator psevaluator(cgalevaluator);
|
|
|
|
|
|
|
|
CGAL_Nef_polyhedron N = cgalevaluator.evaluateCGALMesh(*root_node);
|
|
|
|
|
2011-12-25 03:22:03 +04:00
|
|
|
current_path(original_path);
|
2011-09-01 07:04:42 +04:00
|
|
|
|
2011-09-19 02:53:33 +04:00
|
|
|
// match with csgtest ends
|
2011-10-09 05:02:40 +04:00
|
|
|
try {
|
|
|
|
csgInfo.glview = new OffscreenView(512,512);
|
|
|
|
} catch (int error) {
|
2011-10-11 03:01:51 +04:00
|
|
|
fprintf(stderr,"Can't create OpenGL OffscreenView. Code: %i. Exiting.\n", error);
|
2011-10-09 05:02:40 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
2011-09-01 07:04:42 +04:00
|
|
|
|
2013-01-08 22:26:25 +04:00
|
|
|
CGALRenderer cgalRenderer(N);
|
|
|
|
|
2011-10-16 00:48:41 +04:00
|
|
|
BoundingBox bbox;
|
2011-09-01 19:02:50 +04:00
|
|
|
if (cgalRenderer.polyhedron) {
|
|
|
|
CGAL::Bbox_3 cgalbbox = cgalRenderer.polyhedron->bbox();
|
2011-10-16 05:14:35 +04:00
|
|
|
bbox = BoundingBox(Vector3d(cgalbbox.xmin(), cgalbbox.ymin(), cgalbbox.zmin()),
|
2013-01-08 22:26:25 +04:00
|
|
|
Vector3d(cgalbbox.xmax(), cgalbbox.ymax(), cgalbbox.zmax()));
|
2011-10-16 05:22:06 +04:00
|
|
|
}
|
|
|
|
else if (cgalRenderer.polyset) {
|
2011-09-02 00:43:56 +04:00
|
|
|
bbox = cgalRenderer.polyset->getBoundingBox();
|
|
|
|
}
|
2013-01-08 22:26:25 +04:00
|
|
|
|
2013-03-03 21:41:37 +04:00
|
|
|
Camera cam(Camera::VECTOR);
|
|
|
|
cam.center = getBoundingCenter(bbox);
|
2011-10-16 00:48:41 +04:00
|
|
|
double radius = getBoundingRadius(bbox);
|
2013-01-08 22:26:25 +04:00
|
|
|
|
2011-09-01 19:02:50 +04:00
|
|
|
Vector3d cameradir(1, 1, -0.5);
|
2013-03-03 21:41:37 +04:00
|
|
|
cam.eye = cam.center - radius*2*cameradir;
|
|
|
|
csgInfo.glview->setCamera( cam );
|
|
|
|
|
2011-09-01 07:04:42 +04:00
|
|
|
csgInfo.glview->setRenderer(&cgalRenderer);
|
|
|
|
csgInfo.glview->paintGL();
|
2011-10-16 07:59:54 +04:00
|
|
|
csgInfo.glview->save(outfile);
|
2013-01-08 22:26:25 +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-01 07:04:42 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|