reworked engine structure, added util/ directory with refptr and Solver, set svn:ignore on subdirs
git-svn-id: svn://anubis/jcad/trunk@34 c8684bfa-0c3a-0410-9efb-b8c82542f01e
This commit is contained in:
parent
c18371da07
commit
197bcd7f3d
@ -16,7 +16,7 @@ export CXXFLAGS := -Wall -O2 \
|
|||||||
export LDFLAGS := `pkg-config --libs gtkmm-$(GTKMM_VERSION)` \
|
export LDFLAGS := `pkg-config --libs gtkmm-$(GTKMM_VERSION)` \
|
||||||
`pkg-config --libs gtkglextmm-$(GTKGLEXTMM_VERSION)`
|
`pkg-config --libs gtkglextmm-$(GTKGLEXTMM_VERSION)`
|
||||||
|
|
||||||
SUBDIRS := engine gui main
|
SUBDIRS := util engine gui main
|
||||||
|
|
||||||
.PHONY: $(TARGET)
|
.PHONY: $(TARGET)
|
||||||
$(TARGET):
|
$(TARGET):
|
||||||
|
2
src/engine/Constrainable.cc
Normal file
2
src/engine/Constrainable.cc
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
#include "Constrainable.h"
|
15
src/engine/Constrainable.h
Normal file
15
src/engine/Constrainable.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
#ifndef CONSTRAINABLE_H
|
||||||
|
#define CONSTRAINABLE_H CONSTRAINABLE_H
|
||||||
|
|
||||||
|
#include "util/refptr.h"
|
||||||
|
|
||||||
|
class Constrainable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
protected:
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef refptr<Constrainable> ConstrainableRef;
|
||||||
|
|
||||||
|
#endif
|
2
src/engine/Constraint.cc
Normal file
2
src/engine/Constraint.cc
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
#include "Constraint.h"
|
15
src/engine/Constraint.h
Normal file
15
src/engine/Constraint.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
#ifndef CONSTRAINT_H
|
||||||
|
#define CONSTRAINT_H CONSTRAINT_H
|
||||||
|
|
||||||
|
#include "util/refptr.h"
|
||||||
|
|
||||||
|
class Constraint
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
protected:
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef refptr<Constraint> ConstraintRef;
|
||||||
|
|
||||||
|
#endif
|
19
src/engine/ConstraintSet.cc
Normal file
19
src/engine/ConstraintSet.cc
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
#include "ConstraintSet.h"
|
||||||
|
|
||||||
|
int ConstraintSet::add(refptr<Constrainable> c)
|
||||||
|
{
|
||||||
|
m_objects.push_back(c);
|
||||||
|
return m_objects.size() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ConstraintSet::add(refptr<Constraint> c)
|
||||||
|
{
|
||||||
|
m_constraints.push_back(c);
|
||||||
|
return m_constraints.size() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int solve()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
24
src/engine/ConstraintSet.h
Normal file
24
src/engine/ConstraintSet.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
|
||||||
|
#ifndef CONSTRAINTSET_H
|
||||||
|
#define CONSTRAINTSET_H CONSTRAINTSET_H
|
||||||
|
|
||||||
|
#include "util/refptr.h"
|
||||||
|
#include "Constrainable.h"
|
||||||
|
#include "Constraint.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class ConstraintSet
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int add(ConstrainableRef c);
|
||||||
|
int add(ConstraintRef c);
|
||||||
|
int solve();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::vector<ConstrainableRef> m_objects;
|
||||||
|
std::vector<ConstraintRef> m_constraints;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef refptr<ConstraintSet> ConstraintSetRef;
|
||||||
|
|
||||||
|
#endif
|
@ -1,12 +0,0 @@
|
|||||||
|
|
||||||
#ifndef PROFILE_H
|
|
||||||
#define PROFILE_H
|
|
||||||
|
|
||||||
class Profile
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
protected:
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
23
src/util/Makefile
Normal file
23
src/util/Makefile
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
OBJS := $(patsubst %.cc,%.o,$(wildcard *.cc))
|
||||||
|
DEPS := $(OBJS:.o=.dep)
|
||||||
|
|
||||||
|
all: $(DEPS) $(OBJS)
|
||||||
|
|
||||||
|
%.o: %.cc
|
||||||
|
$(CXX) -c -o $@ $(CPPFLAGS) $(CXXFLAGS) $<
|
||||||
|
|
||||||
|
# Make dependency files
|
||||||
|
%.dep: %.cc
|
||||||
|
@set -e; rm -f $@; \
|
||||||
|
$(CXX) -MM $(CPPFLAGS) $< > $@.$$$$; \
|
||||||
|
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
|
||||||
|
rm -f $@.$$$$
|
||||||
|
|
||||||
|
clean:
|
||||||
|
-$(RM) -f *.o *.dep
|
||||||
|
|
||||||
|
# Include dependency files
|
||||||
|
ifndef CLEAN
|
||||||
|
-include $(DEPS)
|
||||||
|
endif
|
127
src/util/Solver.cc
Normal file
127
src/util/Solver.cc
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
|
||||||
|
#include "Solver.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
/* Generic Solver constructor */
|
||||||
|
Solver::Solver(double a, double b, double c, double d, double e)
|
||||||
|
{
|
||||||
|
this->a = a;
|
||||||
|
this->b = b;
|
||||||
|
this->c = c;
|
||||||
|
this->d = d;
|
||||||
|
this->e = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
Solver::~Solver()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Solver::Result::Result()
|
||||||
|
{
|
||||||
|
numResults = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**************************************************************************
|
||||||
|
* LinearSolver methods *
|
||||||
|
*************************************************************************/
|
||||||
|
LinearSolver::LinearSolver(double a, double b)
|
||||||
|
: Solver(a, b)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/* solve a linear equation */
|
||||||
|
Solver::Result LinearSolver::solve()
|
||||||
|
{
|
||||||
|
/* equation ax + b = 0 */
|
||||||
|
Result res;
|
||||||
|
if (a == 0.0)
|
||||||
|
{
|
||||||
|
if (b == 0.0)
|
||||||
|
{
|
||||||
|
res.numResults = 1;
|
||||||
|
res.results[0] = 0.0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res.numResults = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res.numResults = 1;
|
||||||
|
res.results[0] = -b / a;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**************************************************************************
|
||||||
|
* QuadraticSolver methods *
|
||||||
|
*************************************************************************/
|
||||||
|
QuadraticSolver::QuadraticSolver(double a, double b, double c)
|
||||||
|
: Solver(a, b, c)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/* solve a quadratic equation */
|
||||||
|
Solver::Result QuadraticSolver::solve()
|
||||||
|
{
|
||||||
|
Result res;
|
||||||
|
double discriminant = b * b - 4 * a * c;
|
||||||
|
if (discriminant < 0.0)
|
||||||
|
{
|
||||||
|
res.numResults = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
double sqrt_discriminant = sqrt(discriminant);
|
||||||
|
double two_a = 2.0 * a;
|
||||||
|
if (sqrt_discriminant == 0.0)
|
||||||
|
{
|
||||||
|
res.numResults = 1;
|
||||||
|
res.results[0] = (-b) / two_a;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res.numResults = 2;
|
||||||
|
res.results[0] = (-b - sqrt_discriminant) / two_a;
|
||||||
|
res.results[1] = (-b + sqrt_discriminant) / two_a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**************************************************************************
|
||||||
|
* CubicSolver methods *
|
||||||
|
*************************************************************************/
|
||||||
|
CubicSolver::CubicSolver(double a, double b, double c, double d)
|
||||||
|
: Solver(a, b, c, d)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Solver::Result CubicSolver::solve()
|
||||||
|
{
|
||||||
|
Result res;
|
||||||
|
/* TODO: fill in */
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**************************************************************************
|
||||||
|
* QuarticSolver methods *
|
||||||
|
*************************************************************************/
|
||||||
|
QuarticSolver::QuarticSolver(double a, double b, double c, double d, double e)
|
||||||
|
: Solver(a, b, c, d, e)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Solver::Result QuarticSolver::solve()
|
||||||
|
{
|
||||||
|
Result res;
|
||||||
|
/* TODO: fill in */
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
57
src/util/Solver.h
Normal file
57
src/util/Solver.h
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
|
||||||
|
#ifndef SOLVER_H
|
||||||
|
#define SOLVER_H SOLVER_H
|
||||||
|
|
||||||
|
class Solver
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
class Result
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Result();
|
||||||
|
int numResults;
|
||||||
|
double results[4];
|
||||||
|
};
|
||||||
|
|
||||||
|
Solver(double a = 0.0,
|
||||||
|
double b = 0.0,
|
||||||
|
double c = 0.0,
|
||||||
|
double d = 0.0,
|
||||||
|
double e = 0.0);
|
||||||
|
virtual ~Solver();
|
||||||
|
virtual Result solve() = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
double a, b, c, d, e;
|
||||||
|
};
|
||||||
|
|
||||||
|
class LinearSolver : public Solver
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
LinearSolver(double a, double b);
|
||||||
|
Result solve();
|
||||||
|
};
|
||||||
|
|
||||||
|
class QuadraticSolver : public Solver
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
QuadraticSolver(double a, double b, double c);
|
||||||
|
Result solve();
|
||||||
|
};
|
||||||
|
|
||||||
|
class CubicSolver : public Solver
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CubicSolver(double a, double b, double c, double d);
|
||||||
|
Result solve();
|
||||||
|
};
|
||||||
|
|
||||||
|
class QuarticSolver : public Solver
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
QuarticSolver(double a, double b, double c, double d, double e);
|
||||||
|
Result solve();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
103
src/util/refptr.h
Normal file
103
src/util/refptr.h
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
|
||||||
|
#ifndef REFPTR_H
|
||||||
|
#define REFPTR_H REFPTR_H
|
||||||
|
|
||||||
|
#include <stdlib.h> /* NULL */
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class refptr
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
refptr<T>();
|
||||||
|
refptr<T>(T * ptr);
|
||||||
|
refptr<T>(const refptr<T> & orig);
|
||||||
|
refptr<T> & operator=(const refptr<T> & orig);
|
||||||
|
refptr<T> & operator=(T * ptr);
|
||||||
|
~refptr<T>();
|
||||||
|
T & operator*() const;
|
||||||
|
T * operator->() const;
|
||||||
|
bool isNull() const { return m_ptr == NULL; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void cloneFrom(const refptr<T> & orig);
|
||||||
|
void destroy();
|
||||||
|
|
||||||
|
T * m_ptr;
|
||||||
|
int * m_refCount;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T> refptr<T>::refptr()
|
||||||
|
{
|
||||||
|
m_ptr = NULL;
|
||||||
|
m_refCount = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> refptr<T>::refptr(T * ptr)
|
||||||
|
{
|
||||||
|
m_ptr = ptr;
|
||||||
|
m_refCount = new int;
|
||||||
|
*m_refCount = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> refptr<T>::refptr(const refptr<T> & orig)
|
||||||
|
{
|
||||||
|
cloneFrom(orig);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> refptr<T> & refptr<T>::operator=(const refptr<T> & orig)
|
||||||
|
{
|
||||||
|
destroy();
|
||||||
|
cloneFrom(orig);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> refptr<T> & refptr<T>::operator=(T * ptr)
|
||||||
|
{
|
||||||
|
destroy();
|
||||||
|
m_ptr = ptr;
|
||||||
|
m_refCount = new int;
|
||||||
|
*m_refCount = 1;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> void refptr<T>::cloneFrom(const refptr<T> & orig)
|
||||||
|
{
|
||||||
|
this->m_ptr = orig.m_ptr;
|
||||||
|
this->m_refCount = orig.m_refCount;
|
||||||
|
if (m_refCount != NULL)
|
||||||
|
(*m_refCount)++;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> refptr<T>::~refptr()
|
||||||
|
{
|
||||||
|
destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> void refptr<T>::destroy()
|
||||||
|
{
|
||||||
|
if (m_refCount != NULL)
|
||||||
|
{
|
||||||
|
if (*m_refCount <= 1)
|
||||||
|
{
|
||||||
|
delete m_ptr;
|
||||||
|
delete m_refCount;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
(*m_refCount)--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> T & refptr<T>::operator*() const
|
||||||
|
{
|
||||||
|
return *m_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> T * refptr<T>::operator->() const
|
||||||
|
{
|
||||||
|
return m_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user