Skip to content

Commit

Permalink
Links seem to be working as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
avalero committed Nov 26, 2012
1 parent 4566157 commit 34bb859
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 52 deletions.
14 changes: 2 additions & 12 deletions src/core/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,21 +507,11 @@ class OOMLCore_EXP_DEC Component : public ObjectDecorator
}

inline virtual Links getLinks() const{
if (get()->hasLinks()){
return get()->AbstractObject::getLinks();
}
else{
return get()->getLinks();
}
return get()->getLinks();
}

inline virtual RefSys getLink(int i) const{
if (get()->hasLinks()){
return get()->AbstractObject::getLink(i);
}
else{
return this->get()->getLink(i);
}
return this->get()->getLink(i);
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/core/CompositeObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,23 @@ RefSys CompositeObject::getRefSys() const {

Links CompositeObject::getLinks() const {
if (hasLinks()){
return AbstractObject::getLinks();
Links lks1 = AbstractObject::getLinks();
Links lks2 = _children[0]->getLinks();
lks1.insert( lks1.end(), lks2.begin(), lks2.end() );
return lks1;
//return AbstractObject::getLinks();
}else{
return _children[0]->getLinks();
}
}

RefSys CompositeObject::getLink(int i) const {
if (hasLinks()){
return AbstractObject::getLink(i);
Links lks1 = AbstractObject::getLinks();
Links lks2 = _children[0]->getLinks();
lks1.insert( lks1.end(), lks2.begin(), lks2.end() );
return lks1[i];
//return AbstractObject::getLink(i);
}else{
return _children[0]->getLink(i);
}
Expand Down
6 changes: 6 additions & 0 deletions src/core/RefSys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ RefSys RefSys::transform(TransformMatrix tr){
_trans.transform(tr);
return *this;
}

RefSys RefSys::relTransform(TransformMatrix tr){
_trans.relTransform(tr);
return *this;
}

1 change: 1 addition & 0 deletions src/core/RefSys.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class OOMLCore_EXP_DEC RefSys
//void applyRotation(RotationalMatrix const & rot);

RefSys transform(TransformMatrix tr);
RefSys relTransform(TransformMatrix tr);

inline TransformMatrix getTransformMatrix() const {return _trans;}

Expand Down
42 changes: 24 additions & 18 deletions src/core/TransformDecorator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,33 @@ RefSys TransformDecorator::getRefSys() const{
}

Links TransformDecorator::getLinks() const{
if (hasLinks()){
return AbstractObject::getLinks();
}
else{
Links links = ObjectDecorator::getLinks();
for (int i=0; i<links.size();i++){
links[i].transform(_tr);
}
return links;
}
Links lks1;
Links lks2;

lks1 = ObjectDecorator::getLinks();
for (int i=0; i<lks1.size();i++)
lks1[i].transform(_tr);

if (hasLinks())
lks2 = AbstractObject::getLinks();

lks1.insert( lks1.end(), lks2.begin(), lks2.end() );
return lks1;
}

RefSys TransformDecorator::getLink(int i) const{
if (hasLinks()){
return AbstractObject::getLink(i);
}
else{
RefSys link = ObjectDecorator::getLink(i);
link.transform(_tr);
return link;
}
Links lks1;
Links lks2;

lks1 = ObjectDecorator::getLinks();
for (int j=0; j<lks1.size();j++)
lks1[j].transform(_tr);

if (hasLinks())
lks2 = AbstractObject::getLinks();

lks1.insert( lks1.end(), lks2.begin(), lks2.end() );
return lks1[i];
}

void TransformDecorator::genScad(IndentWriter& writer) const
Expand Down
16 changes: 16 additions & 0 deletions src/core/TransformDecorator.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,27 @@ class OOMLCore_EXP_DEC TransformDecorator : public ObjectDecorator
{

TransformMatrix tr2 = transform->_tr;

//if there are untransformed links apply before transforming
Links lks = transform->AbstractObject::getLinks();
for (int i=0;i<lks.size();i++){
RefSys lk = lks[i];
if (rel)
lk.relTransform(tr2.getInv());
else
lk.transform(tr2.getInv());

transform->get()->addLink(lk);
}


if(rel){
tr = tr2 * tr;
}else{
tr = tr * tr2;
}
double x,y,z;tr.getGlobalXYZAngles(x,y,z);
double ax,ay,az; tr.getGlobalXYZAngles(ax,ay,az);

return SharedPtr<AbstractObject>(new TransformDecorator(transform->get(),tr));
}
Expand Down
31 changes: 31 additions & 0 deletions src/core/TransformMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,33 @@
#include "TransformMatrix.h"
#include "Math.h"

TransformMatrix TransformMatrix::getInv() const {
TransformMatrix inv;

//Traspose the 3x3 submatrix
for (int i=1;i<=3;i++){
for (int j=1;j<=3;j++){
inv.set(i,j,get(j,i));
}
}

//Las row all 0 but last
for (int i=1;i<=3;i++)
inv.set(4,i,0);
inv.set(4,4,1);

double e14 = get(1,4)*get(1,1) + get(2,4)*get(2,1) + get(3,4)*get(3,1) ;
double e24 = get(1,4)*get(1,2) + get(2,4)*get(2,2) + get(3,4)*get(3,2) ;
double e34 = get(1,4)*get(1,3) + get(2,4)*get(2,3) + get(3,4)*get(3,3) ;

inv.set(1,4,-e14);
inv.set(2,4,-e24);
inv.set(3,4,-e34);

return inv;

}

TransformMatrix& TransformMatrix::operator *(TransformMatrix const & matrix){
TransformMatrix* result = new TransformMatrix();

Expand All @@ -40,6 +67,10 @@ void TransformMatrix::transform(TransformMatrix tr){
*this = tr * (*this);
}

void TransformMatrix::relTransform(TransformMatrix tr){
*this = (*this) * tr;
}

void TransformMatrix::rotateEulerZYZ(double z, double yp, double zpp){
rotateZ(z);rotateY(yp);rotateZ(zpp);
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/TransformMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ class OOMLCore_EXP_DEC TransformMatrix: public Matrix<double,4,4>
*/
TransformMatrix & operator*(TransformMatrix const& matrix);

TransformMatrix getInv() const;

void transform(TransformMatrix tr);
void relTransform(TransformMatrix tr);

/**
* \brief Applies an Euler rotation around the initial fixed z, x', z''
Expand Down
36 changes: 36 additions & 0 deletions src/core/Union.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,42 @@

#include <iostream>

Links Union::getLinks() const {
if (hasLinks()){ //If the Union Component has links return all its links and all the children links
Links lks1 = AbstractObject::getLinks();
Links lks2 = _children[0]->getLinks();
lks1.insert( lks1.end(), lks2.begin(), lks2.end() );
return lks1;
}else{ //Return all the children links
Links lks1;
for (int j=0;j<_children.size();j++){
Links lks2 = _children[0]->getLinks();
lks1.insert( lks1.end(), lks2.begin(), lks2.end() );
}
return lks1;
}
}

RefSys Union::getLink(int i) const {
if (hasLinks()){ //If the Union Component has links consider all its links and all the children links
Links lks1 = AbstractObject::getLinks();
for (int j=0;j<_children.size();j++){
Links lks2 = _children[0]->getLinks();
lks1.insert( lks1.end(), lks2.begin(), lks2.end() );
}
return lks1[i];

}else{ //Consider all the children links
Links lks1;
for (int j=0;j<_children.size();j++){
Links lks2 = _children[0]->getLinks();
lks1.insert( lks1.end(), lks2.begin(), lks2.end() );
}
return lks1[i];
}
}


void Union::genScad(IndentWriter& writer) const
{
writer << "union() {" << std::endl;
Expand Down
2 changes: 2 additions & 0 deletions src/core/Union.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class OOMLCore_EXP_DEC Union : public CompositeObject
*/
virtual void printAst(IndentWriter& writer) const;

virtual RefSys getLink(int i) const;
virtual Links getLinks() const;

protected:
/**
Expand Down
33 changes: 13 additions & 20 deletions test/attachment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,31 @@ int main(int argc, char **argv)
std::ofstream file("attachment.scad");
IndentWriter writer;


Component ardu = ArduinoUNO();

/* Create Cube */
Component cube1 = Cube(10,10,10);

/* Add links to the Cube */
cube1.addLink(RefSys(5,-5,-5).relRotate(135,0,0));
cube1.addLink(RefSys(5,5,-5).relRotate(225,0,0));
cube1.addLink(RefSys(5,5,5).relRotate(-45,0,0));
cube1.addLink(RefSys(5,-5,5).relRotate(45,0,0));
cube1.addLink(RefSys(-5,5,5).relRotate(0,0,180).relRotate(45,0,0));
cube1.addLink(RefSys(-5,-5,5).relRotate(0,0,180).relRotate(-45,0,0));
cube1.addLink(RefSys(-5,-5,-5).relRotate(0,180,0).relRotate(45,0,0));
cube1.addLink(RefSys(-5,5,-5).relRotate(0,180,0).relRotate(-45,0,0));
// cube1.addLink(RefSys(5,5,-5).relRotate(225,0,0));
// cube1.addLink(RefSys(5,5,5).relRotate(-45,0,0));
// cube1.addLink(RefSys(5,-5,5).relRotate(45,0,0));
// cube1.addLink(RefSys(-5,5,5).relRotate(0,0,180).relRotate(45,0,0));
// cube1.addLink(RefSys(-5,-5,5).relRotate(0,0,180).relRotate(-45,0,0));
// cube1.addLink(RefSys(-5,-5,-5).relRotate(0,180,0).relRotate(45,0,0));
// cube1.addLink(RefSys(-5,5,-5).relRotate(0,180,0).relRotate(-45,0,0));

//Component cyl = Cylinder(3,50);

//Component comp = cube1 - cyl;

cube1.translate(10,0,0);
cube1.rotate(45,0,0);

Component cyl = Cylinder(2,50);
cube1.translate(5,0,0);

cube1 = cube1 + cyl;
cube1.addLink(RefSys(0,0,0));

cube1.translate(20,40,10);
cube1.translate(5,0,0);
cube1.rotate(0,0,45);

/* Generate OpenSCAD code and write to file */
writer << LinksView(ardu);
writer << ardu;
writer << LinksView(cube1);
writer << cube1;
file << writer ;
file.close();
std::cout << "Done" << std::endl;
Expand Down

0 comments on commit 34bb859

Please sign in to comment.