Structural deformable models
Public Types | Public Member Functions | Public Attributes | Friends | List of all members
SubStructure Class Reference

#include <MStruct.h>

Collaboration diagram for SubStructure:
Collaboration graph
[legend]

Public Types

enum  DistrType { EMD_PCA =0, EMD_GAUSS, EMD_RECT, EMD_RECTTF }
 
enum  Modes { MODE_NONE =0, MODE_NOSPAWN =2, MODE_SHOWSTATS =4 }
 

Public Member Functions

 SubStructure (const std::string &subStructName="", const std::string &supStructName="", const PropTF &transform=PropTF(0.f), int pivot=-1)
 
void clear ()
 
void analyseTF (const DMatrixf &tfmat)
 
EMDistributiongenerateEMDist (const PropVec &wprop, float ppmm, bool inverse=false, dword dtype=EMD_RECT) const
 
void getLBUB (PropVec &lb, PropVec &ub, const PropVec &wprop, float ppmm, float radius=3) const
 
bool read (ParseFile &is)
 

Public Attributes

std::string m_SubStructName
 
std::string m_SupStructName
 
PropTF m_Transform
 
int m_Pivot
 
bool m_Polar
 
PropVec m_Mean
 
PropVec m_Stdev
 
DMatrixf m_PC
 
DMatrixf m_Sigma
 
DMatrixf m_IPC
 
float m_RateWeight
 
float m_RateTH
 
dword m_Mode
 

Friends

std::ostream & operator<< (std::ostream &os, const SubStructure &ss)
 

Detailed Description

Definition at line 21 of file MStruct.h.

Member Enumeration Documentation

Enumerator
EMD_PCA 
EMD_GAUSS 
EMD_RECT 
EMD_RECTTF 

Definition at line 24 of file MStruct.h.

Enumerator
MODE_NONE 
MODE_NOSPAWN 
MODE_SHOWSTATS 

Definition at line 25 of file MStruct.h.

Constructor & Destructor Documentation

SubStructure::SubStructure ( const std::string &  subStructName = "",
const std::string &  supStructName = "",
const PropTF transform = PropTF(0.f),
int  pivot = -1 
)

Definition at line 636 of file MStruct.cpp.

639  : m_SubStructName(subStructName),m_SupStructName(subStructName),
640  m_Transform(transform), m_Pivot(pivot),
643 {
644 }
std::string m_SubStructName
Definition: MStruct.h:41
dword m_Mode
Definition: MStruct.h:48
#define SUBSTRUCT_RATETH
Definition: MStruct.cpp:20
float m_RateWeight
Definition: MStruct.h:47
std::string m_SupStructName
Definition: MStruct.h:41
float m_RateTH
Definition: MStruct.h:47
bool m_Polar
Definition: MStruct.h:44
PropTF m_Transform
Definition: MStruct.h:42
#define SUBSTRUCT_RATEWEIGHT
Definition: MStruct.cpp:18
int m_Pivot
Definition: MStruct.h:43

Member Function Documentation

void SubStructure::analyseTF ( const DMatrixf tfmat)

Definition at line 646 of file MStruct.cpp.

References StructTable::adjustByAvgDir(), DMatrix< T >::at(), dmutil::avg(), DUMP, DMatrix< T >::empty(), dmutil::expand(), DMatrix< T >::getDiag(), DMatrix< T >::getRange(), getSearchPara(), dmutil::inverse(), m_IPC, m_Mean, m_Mode, m_PC, SearcherParams::m_PCTH, m_Sigma, m_Stdev, MODE_SHOWSTATS, DMatrix< T >::mulRight(), PVEC_DIR, DMatrix< T >::resize(), DMatrix< T >::setRange(), VVector< T, D >::size(), DMatrix< T >::sizeX(), DMatrix< T >::sizeY(), dmutil::sqrt(), dmutil::stdev(), dmutil::SVD(), and DMatrix< T >::transpose().

Referenced by MStructure::buildAllStats().

647 {
648  using namespace dmutil;
649  if(tfmat.empty() ||tfmat.sizeX()!=PropVec::size()) {
650  m_PC = m_IPC = makeIdentity<float>(PropVec::size()+1);
651  m_Sigma.resize(1,4); m_Sigma = 0.f;
652  m_Mean = m_Stdev = 0.f;
653  return;
654  }
655  bool show = false && (m_Mode&MODE_SHOWSTATS);
656  if(show) DUMP(tfmat);
657  DMatrixf ntfmat(tfmat);
658  DMatrixf angles(1,ntfmat.sizeY());
659  ntfmat.getRange(PVEC_DIR, 0, angles);
661  ntfmat.setRange(PVEC_DIR, 0, angles);
662  //todo: switch to polar position (translation)
663 // ofstream f1("tfmat.dat");
664 // f1 << tfmat;
665 // ofstream f2("ids.dat");
666 // copy(comod.begin(), comod.end(), ostream_iterator<dword>(f2, "\n"));
667  //build a (0,1) standardized matrix of the distribution
668  DMatrixf avghg(avg(ntfmat));
669  DMatrixf stdevhg(stdev(ntfmat));
670  DMatrixf mavg(expand(avghg, 1,ntfmat.sizeY()));
671  DMatrixf mstdev(expand(stdevhg, 1,ntfmat.sizeY()));
672  ntfmat -= mavg; ntfmat /= mstdev;
673  //build a normalized covariance matrix
674  DMatrixf tfcov(ntfmat);
675  tfcov.transpose();
676  tfcov = tfcov.mulRight(ntfmat);
677  tfcov *= (1.f/(ntfmat.sizeY())); //normalize by number of samples
678  if(show) DUMP(tfcov);
679  DMatrixf mU, mS, mV;
680  dmutil::SVD(tfcov, mU, mS, mV); // TODO
681  m_Sigma = mS.getDiag();
682  float relvar = 1.f/4; //1.f/dmutil::sum(m_Sigma).at(0,0); // has to be 1/4
683  for(dword i=0;i<m_Sigma.sizeY(); i++)
684  if(m_Sigma.at(0,i)*relvar < getSearchPara().m_PCTH)
685  m_Sigma.at(0,i) = 0.f;
687  m_Mean = &avghg.at(0,0);
688  m_Stdev = &stdevhg.at(0,0);
689  if(show) DUMP(mU << mS << mV);
690 //make a homogeneous matrix to integrate de-standardization
691  //m_Sigma.resize(1,m_Sigma.sizeY()+1,1.f);
692  //mS = makeDiag(m_Sigma);
693 // avghg.transpose();
694 // avghg.resize(1,avghg.sizeY()+1,1.f);
695 // stdevhg.transpose();
696 // stdevhg.resize(1,stdevhg.sizeY()+1,1.f);
697 // DMatrixf mVhg(makeIdentity<float>(mV.sizeX()+1));
698 // mVhg.setRange(0,0,mV);
699 // //mVhg.transpose(); //no transposed, or not?
700 // //m_PC = makeDiag(stdevhg).mulRight(mVhg.mulRight(mS));
701 // m_PC = makeDiag(stdevhg).mulRight(mVhg);
702 // m_PC.setRange(m_PC.sizeX()-1,0,avghg);
703  m_PC = mV;
704  m_IPC = inverse(m_PC);
705  //cout << m_Sigma << m_Mean << endl << m_Stdev << endl;
706 // DUMP(m_SubStructName);
707 // DUMP(m_Mean);
708 // DUMP(m_Stdev);
709 }
#define DUMP(expr)
Definition: common.h:16
DMatrix< T > inverse(const DMatrix< T > &dmat)
Definition: DMatrixLinAlg.h:67
dword m_Mode
Definition: MStruct.h:48
MT getDiag(int offset=0)
Definition: DMatrix.h:231
bool empty() const
Definition: DMatrix.h:50
float m_PCTH
threshold for principle components
Definition: Searcher.h:188
DMatrixf m_PC
Definition: MStruct.h:46
dword sizeX() const
Definition: DMatrix.h:42
DMatrixf m_Sigma
Definition: MStruct.h:46
bool SVD(const DMatrix< T > &dmat, DMatrix< T > &mU, DMatrix< T > &mS, DMatrix< T > &mV)
Definition: DMatrixLinAlg.h:74
MT & resize(dword _sx, dword _sy, const T &inival=T())
Definition: DMatrix.h:30
SearcherParams & getSearchPara()
Definition: Searcher.h:194
unsigned long dword
Definition: simpletypes.h:6
static DMatrixf & adjustByAvgDir(DMatrixf &mat)
DMatrix< T > avg(const DMatrix< T > &mat)
Definition: DMatrixUtil.h:90
DMatrix< T > stdev(const DMatrix< T > &mat)
Definition: DMatrixUtil.h:106
dword sizeY() const
Definition: DMatrix.h:43
T & at(dword x, dword y)
Definition: DMatrix.h:46
MT & getRange(dword ox, dword oy, MT &mat) const
Definition: DMatrix.h:197
DMatrix< T > expand(const DMatrix< T > &mat, dword mx, dword my)
Definition: DMatrixUtil.h:113
PropVec m_Stdev
Definition: MStruct.h:45
static unsigned int size()
Definition: VVector.h:41
DMatrixf m_IPC
Definition: MStruct.h:46
DMatrix< T > & sqrt(DMatrix< T > &mat)
Definition: DMatrixUtil.h:81
PropVec m_Mean
Definition: MStruct.h:45
void SubStructure::clear ( )

Definition at line 822 of file MStruct.cpp.

References m_Mode, m_Pivot, m_Polar, m_RateTH, m_RateWeight, m_SubStructName, m_SupStructName, m_Transform, MODE_NONE, SUBSTRUCT_RATETH, and SUBSTRUCT_RATEWEIGHT.

Referenced by read().

823 {
824  m_SubStructName.clear();
825  m_SupStructName.clear();
826  m_Transform = 0.f;
827  m_Pivot = -1;
828  m_Polar = false;
829  m_Mode = MODE_NONE;
832 }
std::string m_SubStructName
Definition: MStruct.h:41
dword m_Mode
Definition: MStruct.h:48
#define SUBSTRUCT_RATETH
Definition: MStruct.cpp:20
float m_RateWeight
Definition: MStruct.h:47
std::string m_SupStructName
Definition: MStruct.h:41
float m_RateTH
Definition: MStruct.h:47
bool m_Polar
Definition: MStruct.h:44
PropTF m_Transform
Definition: MStruct.h:42
#define SUBSTRUCT_RATEWEIGHT
Definition: MStruct.cpp:18
int m_Pivot
Definition: MStruct.h:43
EMDistribution * SubStructure::generateEMDist ( const PropVec wprop,
float  ppmm,
bool  inverse = false,
dword  dtype = EMD_RECT 
) const

Definition at line 711 of file MStruct.cpp.

References DMatrix< T >::at(), ExpectationMap::correctLBUB(), EMD_GAUSS, EMD_PCA, EMD_RECT, EMD_RECTTF, DMatrix< T >::empty(), fwdPropTF(), getIdentityPropTF(), getPropPos(), getSearchPara(), invPropTF(), m_IPC, m_Mean, m_PC, SearcherParams::m_ScaleStd, m_Sigma, m_Stdev, DMatrix< T >::mulLeft(), NULL, PVEC_POSX, PVEC_POSY, EMDXformer::setMean(), setPropPos(), DMatrix< T >::sizeX(), and dmutil::sqrt().

Referenced by MStructure::addExpectation(), and MStructure::showStats().

714 {
715  //ATTENTION: backward transform isn't working the way I'm doing it here
716  // so, only use forward transform
717  EMDistribution* emdist = NULL;
718  switch(DistrType(dtype)) {
719  case EMD_PCA:
720  {
721  if(m_Sigma.empty()) break;
722  //generate a transformed gaussian distribution (using PCA xform)
723  DMatrixf stddm(m_Sigma);
724  stddm *= getSearchPara().m_ScaleStd;
725  PropVec stdtf(&stddm.at(0,0));
726  EMDGauss* eminput = new EMDGauss(PropVec(0.f), stdtf);
727  //EMDRect* eminput = new EMDRect(stdtf*-1.f, stdtf);
728  DMatrixf tfmat = backwards ? m_IPC : m_PC;
729 #ifndef _PROPTF_NORMALIZE_SCALE_
730  DMatrixf mmmat = dmutil::makeIdentity<float>(tfmat.sizeX());
731  mmmat.at(PVEC_POSX,PVEC_POSX) = ppmm;
732  mmmat.at(PVEC_POSY,PVEC_POSY) = ppmm;
733  tfmat = tfmat.mulLeft(mmmat);
734 #endif
735  emdist = (EMDistribution*) new EMDXformer(eminput, wprop, tfmat);
736  ((EMDXformer*)emdist)->setMean(m_Mean);
737  ((EMDXformer*)emdist)->setStdev(m_Stdev);
738  }
739  break;
740  case EMD_RECT:
741  {
742  //generate a rectangular uniform distribution
743  PropVec propsd(m_Stdev);
744  //propsd*=sqrt(3.f);
745  PropVec lb(m_Mean-propsd);
746  PropVec ub(m_Mean+propsd);
747 #ifndef _PROPTF_NORMALIZE_SCALE_
748  setPropPos(lb, getPropPos(lb)*ppmm);
749  setPropPos(ub, getPropPos(ub)*ppmm);
750 #endif
751  PropVec olb(wprop), oub(wprop);
752  if(backwards) { invPropTF(olb, ub); invPropTF(oub, lb); }
753  else { fwdPropTF(olb, lb); fwdPropTF(oub, ub); }
754  PropVec mean(m_Mean);
755  setPropPos(mean, getPropPos(mean)*ppmm);
756  PropVec a(wprop);
757  ExpectationMap::correctLBUB(olb, oub);
758  emdist = (EMDistribution*) new EMDRect(olb,oub);
759  }
760  break;
761  case EMD_RECTTF:
762  {
763  //generate a rectangular uniform distribution
764  PropVec propsd(m_Stdev);
765  propsd*=sqrt(3.f);
766  PropVec lb(m_Mean-propsd);
767  PropVec ub(m_Mean+propsd);
768 #ifndef _PROPTF_NORMALIZE_SCALE_
769  setPropPos(lb, getPropPos(lb)*ppmm);
770  setPropPos(ub, getPropPos(ub)*ppmm);
771 #endif
773  if(backwards) { invPropTF(olb, lb); invPropTF(oub, ub); }
774  else { fwdPropTF(olb, lb); fwdPropTF(oub, ub); }
775  ExpectationMap::correctLBUB(olb, oub);
776  EMDistribution* inpdist = (EMDistribution*) new EMDRect(olb,oub);
777  emdist = (EMDistribution*) new EMDXformer(inpdist, wprop);
778  }
779  break;
780  case EMD_GAUSS:
781  {
782  //generate a multidimensional axis-aligned gaussian distribution
783  PropVec sdevpv(m_Stdev);
784  sdevpv *= getSearchPara().m_ScaleStd;
785  PropVec meanpv(m_Mean);
786 #ifndef _PROPTF_NORMALIZE_SCALE_
787  setPropPos(sdevpv, getPropPos(sdevpv)*ppmm);
788  setPropPos(meanpv, getPropPos(meanpv)*ppmm);
789 #endif
790  PropVec cprop(getIdentityPropTF());
791  if(backwards) invPropTF(cprop, meanpv);
792  else fwdPropTF(cprop, meanpv); //cool form of: cprop = meanpv
793  EMDistribution* inpdist =
794  (EMDistribution*) new EMDGauss(PropVec(0.f), sdevpv);
795  emdist = (EMDistribution*) new EMDXformer(inpdist, wprop);
796  ((EMDXformer*)emdist)->setMean(cprop);
797  }
798  break;
799  }
800  return emdist;
801 }
#define NULL
Definition: simpletypes.h:9
bool empty() const
Definition: DMatrix.h:50
Point2D getPropPos(const PropVec &prop)
Definition: PropVec.h:13
DMatrixf m_PC
Definition: MStruct.h:46
VVector< float, 4 > PropVec
Definition: PropVec.h:8
PropVec & invPropTF(PropVec &to, const PropTF &tf)
Definition: PropVec.h:66
static void correctLBUB(PropVec &lb, PropVec &ub)
Definition: ExpMap.cpp:193
PropTF getIdentityPropTF()
Definition: PropVec.h:81
float m_ScaleStd
scale stdev of link
Definition: Searcher.h:176
PropVec & setPropPos(PropVec &prop, const Point2D &p)
Definition: PropVec.h:16
void setMean(const PropVec &mean)
Definition: ExpMap.h:134
Definition: ExpMap.h:73
dword sizeX() const
Definition: DMatrix.h:42
DMatrixf m_Sigma
Definition: MStruct.h:46
SearcherParams & getSearchPara()
Definition: Searcher.h:194
MT mulLeft(const MT &rhs) const
Definition: DMatrix.h:159
PropVec & fwdPropTF(PropVec &from, const PropTF &tf)
Definition: PropVec.h:51
T & at(dword x, dword y)
Definition: DMatrix.h:46
PropVec m_Stdev
Definition: MStruct.h:45
DMatrixf m_IPC
Definition: MStruct.h:46
DMatrix< T > & sqrt(DMatrix< T > &mat)
Definition: DMatrixUtil.h:81
PropVec m_Mean
Definition: MStruct.h:45
void SubStructure::getLBUB ( PropVec lb,
PropVec ub,
const PropVec wprop,
float  ppmm,
float  radius = 3 
) const

Definition at line 803 of file MStruct.cpp.

References fwdPropTF(), getPropPos(), getPropScale(), m_Mean, M_PI, m_Stdev, setPropDir(), setPropPos(), and setPropScale().

Referenced by MStructure::addExpectation().

805 {
806  lb = ub = wprop;
807  PropVec tflb(m_Mean);
808  tflb -= (m_Stdev*radius);
809  PropVec tfub(m_Mean);
810  tfub += (m_Stdev*radius);
811 #ifndef _PROPTF_NORMALIZE_SCALE_
812  setPropPos(tflb, getPropPos(tflb)*ppmm);
813  setPropPos(tfub, getPropPos(tfub)*ppmm);
814 #endif
815  fwdPropTF(lb, tflb);
816  fwdPropTF(ub, tfub);
817  if(getPropScale(lb)<0.001) setPropScale(lb, 0.001);
818  setPropDir(lb, 0);
819  setPropDir(ub, 2*M_PI);
820 }
Point2D getPropPos(const PropVec &prop)
Definition: PropVec.h:13
float getPropScale(const PropVec &prop)
Definition: PropVec.h:19
PropVec & setPropDir(PropVec &prop, float dir)
Definition: PropVec.h:28
PropVec & setPropPos(PropVec &prop, const Point2D &p)
Definition: PropVec.h:16
#define M_PI
Definition: mathutil.h:9
PropVec & fwdPropTF(PropVec &from, const PropTF &tf)
Definition: PropVec.h:51
PropVec & setPropScale(PropVec &prop, float pscale)
Definition: PropVec.h:22
PropVec m_Stdev
Definition: MStruct.h:45
PropVec m_Mean
Definition: MStruct.h:45
bool SubStructure::read ( ParseFile is)

Definition at line 834 of file MStruct.cpp.

References clear(), ParseFile::error(), fromString(), ParseFile::getKey(), ParseFile::getNextLine(), ParseFile::getValue(), m_Mode, m_Pivot, m_RateTH, m_RateWeight, m_SubStructName, m_Transform, MODE_NOSPAWN, MODE_SHOWSTATS, and ParseFile::setParseError().

Referenced by MStructure::hasSupStruct(), and MStructure::read().

835 {
836  if(is.getKey() != "substruct") return false;
837  clear();
838  m_SubStructName = is.getValue();
839  bool readon = true;
840  while(readon && is.getNextLine()) {
841  const string& key = is.getKey();
842  const string& value = is.getValue();
843  if(key == "transform") {
844  fromString(value, m_Transform);
845  } else if(key == "pivot") {
846  fromString(value, m_Pivot);
847  m_Pivot--;
848  } else if(key == "rateweight") {
849  fromString(value, m_RateWeight);
850  } else if(key == "rateth") {
851  fromString(value, m_RateTH);
852  } else if(key == "mode") {
853  if(value.find("nospawn") != value.npos)
854  m_Mode |= MODE_NOSPAWN;
855  if(value.find("showstats") != value.npos)
857  } else if (key == "end") readon = false;
858  else { is.setParseError("error parsing substructure"); return false; }
859  }
860  if(is.error()) return false;
861  return true;
862 }
std::string m_SubStructName
Definition: MStruct.h:41
T & fromString(const std::string &str, T &v)
Definition: utils.h:67
dword m_Mode
Definition: MStruct.h:48
const std::string & getValue() const
Definition: ParseFile.h:57
bool error() const
Definition: ParseFile.h:50
float m_RateWeight
Definition: MStruct.h:47
void clear()
Definition: MStruct.cpp:822
float m_RateTH
Definition: MStruct.h:47
const std::string & getKey() const
Definition: ParseFile.h:56
PropTF m_Transform
Definition: MStruct.h:42
void setParseError(const std::string &msg="")
Definition: ParseFile.h:80
int m_Pivot
Definition: MStruct.h:43
const bool getNextLine()
Definition: ParseFile.h:59

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const SubStructure ss 
)
friend

Definition at line 864 of file MStruct.cpp.

865 {
866  os << " substruct " << ss.m_SubStructName << endl;
867  os << " transform " << ss.m_Transform << endl;
868  if(ss.m_Pivot>=0)
869  os << " pivot " << ss.m_Pivot+1 << endl;
870  if(ss.m_Polar) os << " polar" << endl;
872  os << " rateweight " << ss.m_RateWeight << endl;
873  if(ss.m_Mode != SubStructure::MODE_NONE) {
874  os << " mode"
875  << (ss.m_Mode&SubStructure::MODE_NOSPAWN ? " nospawn" : "")
876  << (ss.m_Mode&SubStructure::MODE_SHOWSTATS ? " showstats" : "")
877  << endl;
878  }
879  os << " end # of substruct" << endl;
880  return os;
881 }
std::string m_SubStructName
Definition: MStruct.h:41
dword m_Mode
Definition: MStruct.h:48
float m_RateWeight
Definition: MStruct.h:47
bool m_Polar
Definition: MStruct.h:44
PropTF m_Transform
Definition: MStruct.h:42
#define SUBSTRUCT_RATEWEIGHT
Definition: MStruct.cpp:18
int m_Pivot
Definition: MStruct.h:43

Member Data Documentation

DMatrixf SubStructure::m_IPC

Definition at line 46 of file MStruct.h.

Referenced by analyseTF(), and generateEMDist().

PropVec SubStructure::m_Mean

Definition at line 45 of file MStruct.h.

Referenced by analyseTF(), generateEMDist(), getLBUB(), and MStructure::showStats().

dword SubStructure::m_Mode
DMatrixf SubStructure::m_PC

Definition at line 46 of file MStruct.h.

Referenced by analyseTF(), and generateEMDist().

int SubStructure::m_Pivot
bool SubStructure::m_Polar

Definition at line 44 of file MStruct.h.

Referenced by clear(), and operator<<().

float SubStructure::m_RateTH

Definition at line 47 of file MStruct.h.

Referenced by clear(), StructTable::findBestConnection(), and read().

float SubStructure::m_RateWeight
DMatrixf SubStructure::m_Sigma

Definition at line 46 of file MStruct.h.

Referenced by analyseTF(), generateEMDist(), and MStructure::showStats().

PropVec SubStructure::m_Stdev

Definition at line 45 of file MStruct.h.

Referenced by analyseTF(), generateEMDist(), getLBUB(), and MStructure::showStats().

std::string SubStructure::m_SubStructName
std::string SubStructure::m_SupStructName

Definition at line 41 of file MStruct.h.

Referenced by clear(), and MStructure::showStats().

PropTF SubStructure::m_Transform

Definition at line 42 of file MStruct.h.

Referenced by clear(), operator<<(), and read().


The documentation for this class was generated from the following files: