Permalink
Browse files

Lua for snapPoints and nearestPointOnPath

  • Loading branch information...
feragon committed Jul 6, 2017
1 parent 038e4dc commit 9d09b9d6db5abd344627ec0f4af4ce7d8d4a9e11
@@ -12,6 +12,8 @@ set(lcluascript_srcs
lckernelbridge.cpp
managers/pluginmanager.cpp
lclua.cpp
primitive/customentity.cpp
builders/customentity.cpp
)
# HEADER FILES
@@ -20,6 +22,8 @@ set(lcluascript_hdrs
utils/timer.h
managers/pluginmanager.h
lclua.h
primitive/customentity.h
builders/customentity.h
)
@@ -0,0 +1,41 @@
#include "customentity.h"
#include "../primitive/customentity.h"
using namespace lc;
using namespace builder;
CustomEntityBuilder::CustomEntityBuilder() {
}
void CustomEntityBuilder::setSnapFunction(LuaIntf::LuaRef snapFunction) {
_snapFunction = snapFunction;
}
entity::LuaCustomEntity_CSPtr CustomEntityBuilder::build() {
if(!checkValues()) {
throw "Missing or invalid values";
}
return entity::LuaCustomEntity_CSPtr(new entity::LuaCustomEntity(*this));
}
const LuaIntf::LuaRef& CustomEntityBuilder::snapFunction() const {
return _snapFunction;
}
bool CustomEntityBuilder::checkValues() {
return InsertBuilder::checkValues() &&
_snapFunction.isValid() && _snapFunction.isFunction() &&
_nearestPointFunction.isValid() && _nearestPointFunction.isFunction();
}
const LuaIntf::LuaRef& CustomEntityBuilder::nearestPointFunction() const {
return _nearestPointFunction;
}
void CustomEntityBuilder::setNearestPointFunction(const LuaIntf::LuaRef& nearestPointFunction) {
_nearestPointFunction = nearestPointFunction;
}
@@ -0,0 +1,31 @@
#pragma once
#include <lclua.h>
#include <cad/builders/insert.h>
namespace lc {
namespace entity {
class LuaCustomEntity;
DECLARE_SHORT_SHARED_PTR(LuaCustomEntity)
}
namespace builder {
class CustomEntityBuilder : public InsertBuilder {
public:
CustomEntityBuilder();
void setSnapFunction(LuaIntf::LuaRef snapFunction);
const LuaIntf::LuaRef& snapFunction() const;
const LuaIntf::LuaRef& nearestPointFunction() const;
void setNearestPointFunction(const LuaIntf::LuaRef& nearestPointFunction);
bool checkValues() override;
entity::LuaCustomEntity_CSPtr build();
private:
LuaIntf::LuaRef _snapFunction;
LuaIntf::LuaRef _nearestPointFunction;
};
}
}
@@ -17,6 +17,7 @@
#include <cad/operations/blockops.h>
#include <cad/operations/builder.h>
#include <cad/primitive/insert.h>
#include <primitive/customentity.h>
#include "lclua.h"
using namespace LuaIntf;
@@ -168,6 +169,10 @@ void LCLua::importLCKernel() {
.endClass()
.beginClass<Snapable>("Snapable")
.addFunction("snapPoints", &Snapable::snapPoints)
.addFunction("nearestPointOnPath", &Snapable::nearestPointOnPath)
.addStaticFunction("remove_ifDistanceGreaterThen", &Snapable::remove_ifDistanceGreaterThen)
.addStaticFunction("snapPointsCleanup", &Snapable::snapPointsCleanup)
.endClass()
.beginClass<ID>("ID")
@@ -595,5 +600,45 @@ void LCLua::importLCKernel() {
.addFunction("position", &entity::Insert::position)
.addFunction("document", &entity::Insert::document)
.endClass()
.beginExtendClass<entity::CustomEntity, entity::Insert>("CustomEntity")
.endClass()
.beginExtendClass<entity::LuaCustomEntity, entity::CustomEntity>("LuaCustomEntity")
.endClass()
.beginExtendClass<builder::CustomEntityBuilder, builder::InsertBuilder>("CustomEntityBuilder")
.addConstructor(LUA_ARGS())
.addFunction("snapFunction", &builder::CustomEntityBuilder::snapFunction)
.addFunction("setSnapFunction", &builder::CustomEntityBuilder::setSnapFunction)
.addFunction("nearestPointFunction", &builder::CustomEntityBuilder::nearestPointFunction)
.addFunction("setNearestPointFunction", &builder::CustomEntityBuilder::setNearestPointFunction)
.addFunction("checkValues", &builder::CustomEntityBuilder::checkValues)
.addFunction("build", &builder::CustomEntityBuilder::build)
.endClass()
.beginClass<lc::SimpleSnapConstrain>("SimpleSnapConstrain")
.addConstant("NONE", SimpleSnapConstrain::NONE)
.addConstant("ON_ENTITY", SimpleSnapConstrain::ON_ENTITY)
.addConstant("ON_ENTITYPATH", SimpleSnapConstrain::ON_ENTITYPATH)
.addConstant("ENTITY_CENTER", SimpleSnapConstrain::ENTITY_CENTER)
.addConstant("LOGICAL", SimpleSnapConstrain::LOGICAL)
.addConstant("DIVIDED", SimpleSnapConstrain::DIVIDED)
.addFunction("constrain", &SimpleSnapConstrain::constrain)
.addFunction("divisions", &SimpleSnapConstrain::divisions)
.addFunction("angle", &SimpleSnapConstrain::angle)
.addFunction("setDivisions", &SimpleSnapConstrain::setDivisions)
.addFunction("setAngle", &SimpleSnapConstrain::setAngle)
.addFunction("enableConstrain", &SimpleSnapConstrain::enableConstrain)
.addFunction("disableConstrain", &SimpleSnapConstrain::disableConstrain)
.addFunction("hasConstrain", &SimpleSnapConstrain::hasConstrain)
.endClass()
.beginClass<EntityCoordinate>("EntityCoordinate")
.addConstructor(LUA_ARGS(geo::Coordinate, int))
.addFunction("coordinate", &EntityCoordinate::coordinate)
.addFunction("pointId", &EntityCoordinate::pointId)
.endClass()
;
}
@@ -0,0 +1,27 @@
#include <builders/customentity.h>
#include "customentity.h"
using namespace lc;
using namespace entity;
LuaCustomEntity::LuaCustomEntity(const lc::builder::CustomEntityBuilder& builder) :
CustomEntity(builder),
_snapPoints(builder.snapFunction()),
_nearestPoint(builder.nearestPointFunction()) {
}
std::vector<EntityCoordinate> LuaCustomEntity::snapPoints(const geo::Coordinate& coord,
const SimpleSnapConstrain& simpleSnapConstrain,
double minDistanceToSnap,
int maxNumberOfSnapPoints) const {
auto snapPointsDupl = _snapPoints; //Dirty hack to call Lua function from a const function
auto points = snapPointsDupl.call<std::vector<EntityCoordinate>>(coord, simpleSnapConstrain, minDistanceToSnap, maxNumberOfSnapPoints);
Snapable::snapPointsCleanup(points, coord, maxNumberOfSnapPoints, minDistanceToSnap);
return points;
}
geo::Coordinate LuaCustomEntity::nearestPointOnPath(const geo::Coordinate& coord) const {
auto nearestPointDupl = _nearestPoint;
return nearestPointDupl.call<geo::Coordinate>(coord);
}
@@ -0,0 +1,27 @@
#pragma once
#include "../builders/customentity.h"
#include <cad/primitive/customentity.h>
#include <cad/interface/snapable.h>
namespace lc {
namespace entity {
class LuaCustomEntity : public CustomEntity {
public:
LuaCustomEntity(const builder::CustomEntityBuilder& builder);
virtual std::vector<EntityCoordinate> snapPoints(const geo::Coordinate& coord,
const SimpleSnapConstrain& simpleSnapConstrain,
double minDistanceToSnap,
int maxNumberOfSnapPoints) const override;
virtual geo::Coordinate nearestPointOnPath(const geo::Coordinate& coord) const override;
private:
LuaIntf::LuaRef _snapPoints;
LuaIntf::LuaRef _nearestPoint;
};
DECLARE_SHORT_SHARED_PTR(LuaCustomEntity)
}
}
View
@@ -73,6 +73,8 @@ cad/builders/linepattern.cpp
cad/meta/customentitystorage.cpp
cad/operations/blockops.cpp
cad/operations/builder.cpp
cad/primitive/customentity.cpp
cad/interface/snapconstrain.cpp
)
# HEADER FILES
@@ -168,6 +170,7 @@ cad/meta/customentitystorage.h
cad/operations/blockops.h
cad/operations/builder.h
cad/base/visitor.h
cad/primitive/customentity.h
)
# LOG4CXX
@@ -0,0 +1,10 @@
#include "snapconstrain.h"
using namespace lc;
const uint16_t SimpleSnapConstrain::NONE = 0x00;
const uint16_t SimpleSnapConstrain::ON_ENTITY = 0x01;
const uint16_t SimpleSnapConstrain::ON_ENTITYPATH = 0x02;
const uint16_t SimpleSnapConstrain::ENTITY_CENTER = 0x04;
const uint16_t SimpleSnapConstrain::LOGICAL = 0x08;
const uint16_t SimpleSnapConstrain::DIVIDED = 0x10;
@@ -14,14 +14,14 @@ namespace lc {
*/
class SimpleSnapConstrain {
public:
const static uint16_t NONE = 0x00;
const static uint16_t ON_ENTITY = 0x01; // Find a point anywhere on it's entity.
const static uint16_t ON_ENTITYPATH = 0x02; // Find a point anywhere on it's entity's path
const static uint16_t ENTITY_CENTER = 0x04; // Get a snap point centered along the entity. For a arc with will be in the middle between arc start and arc end, for line it will be the center, for a circle this is not possible.
const static uint16_t NONE;
const static uint16_t ON_ENTITY; // Find a point anywhere on it's entity.
const static uint16_t ON_ENTITYPATH; // Find a point anywhere on it's entity's path
const static uint16_t ENTITY_CENTER; // Get a snap point centered along the entity. For a arc with will be in the middle between arc start and arc end, for line it will be the center, for a circle this is not possible.
// ENTITY_GRAVCENTER = 8; // Entities gravitationally center, for a circle this would be the circle's center
const static uint16_t LOGICAL = 0x08; // enable to entities 'handles' There can be for example end point's of a line, or the center of a circle, or the 4 corners of a circle, grid point's etc..
const static uint16_t LOGICAL; // enable to entities 'handles' There can be for example end point's of a line, or the center of a circle, or the 4 corners of a circle, grid point's etc..
// basically anything that might makes sense to snap into.
const static uint16_t DIVIDED = 0x10; // DIVIDED divides the entity into X equal portions given by divisions
const static uint16_t DIVIDED; // DIVIDED divides the entity into X equal portions given by divisions
SimpleSnapConstrain &operator=(const SimpleSnapConstrain &other) {
@@ -72,6 +72,10 @@ namespace lc {
return SimpleSnapConstrain(_constrain & ~constrain, _divisions, _angle);
}
bool hasConstrain(uint16_t constrain) const {
return (bool) (_constrain & constrain);
}
private:
uint16_t _constrain;
@@ -0,0 +1,5 @@
#include "customentity.h"
lc::entity::CustomEntity::CustomEntity(const lc::builder::InsertBuilder& builder) : Insert(builder) {
}
@@ -0,0 +1,19 @@
#pragma once
#include "insert.h"
namespace lc {
namespace entity {
class CustomEntity : public Insert {
public:
CustomEntity(const builder::InsertBuilder& builder);
virtual std::vector<EntityCoordinate> snapPoints(const geo::Coordinate& coord,
const SimpleSnapConstrain& simpleSnapConstrain,
double minDistanceToSnap,
int maxNumberOfSnapPoints) const override = 0;
virtual geo::Coordinate nearestPointOnPath(const geo::Coordinate& coord) const override = 0;
};
}
}
@@ -139,7 +139,7 @@ void Insert::calculateBoundingBox() {
it++;
while (it != entities.end()) {
_boundingBox.merge((*it)->move(offset)->boundingBox());
_boundingBox = _boundingBox.merge((*it)->move(offset)->boundingBox());
it++;
}
}
@@ -34,13 +34,15 @@ namespace lc {
std::map<unsigned int, geo::Coordinate> dragPoints() const override;
CADEntity_CSPtr setDragPoints(std::map<unsigned int, lc::geo::Coordinate> dragPoints) const override;
std::vector<EntityCoordinate> snapPoints(const geo::Coordinate& coord, const SimpleSnapConstrain& simpleSnapConstrain,
virtual std::vector<EntityCoordinate> snapPoints(const geo::Coordinate& coord, const SimpleSnapConstrain& simpleSnapConstrain,
double minDistanceToSnap, int maxNumberOfSnapPoints) const override;
geo::Coordinate nearestPointOnPath(const geo::Coordinate& coord) const override;
virtual geo::Coordinate nearestPointOnPath(const geo::Coordinate& coord) const override;
private:
protected:
Insert(const builder::InsertBuilder& builder);
private:
void calculateBoundingBox();
void on_addEntityEvent(const lc::AddEntityEvent&);

0 comments on commit 9d09b9d

Please sign in to comment.