diff --git a/include/ArrayTree/ArrayTreeBuilder.tpp b/include/ArrayTree/ArrayTreeBuilder.tpp index 2996ffa..5ba8cac 100644 --- a/include/ArrayTree/ArrayTreeBuilder.tpp +++ b/include/ArrayTree/ArrayTreeBuilder.tpp @@ -50,6 +50,7 @@ ArrayTreeBuilder::buildTree (Tree &tree, WeightAttributes::getValues () { return values; } +template +inline void +AttributeProfiles::printValues () const { + cout << "AP" << endl; + const Size doubleSize (tree.getSize().width, 2*tree.getSize ().height); + printMap (cout, values, doubleSize, tree.getNodeCount ()) << endl << endl; +} + template inline void AttributeProfiles::free () { diff --git a/include/Attributes/AreaAttributes.hpp b/include/Attributes/AreaAttributes.hpp index 073371b..851c30a 100644 --- a/include/Attributes/AreaAttributes.hpp +++ b/include/Attributes/AreaAttributes.hpp @@ -7,13 +7,12 @@ namespace otb { namespace triskele { - template - class AreaAttributes : public CompAttributeC { + class AreaAttributes : public CompAttributeC { public: - inline AreaAttributes (const Tree &tree, const WeightAttributes &weightAttributes, const unsigned int &treeCoreCount); + inline AreaAttributes (const Tree &tree, const unsigned int &treeCoreCount); inline ~AreaAttributes (); protected: - inline void compute (const WeightAttributes &weightAttributes); + inline void compute (); }; #include "AreaAttributes.tpp" diff --git a/include/Attributes/AreaAttributes.tpp b/include/Attributes/AreaAttributes.tpp index f5c38b4..6f36da0 100644 --- a/include/Attributes/AreaAttributes.tpp +++ b/include/Attributes/AreaAttributes.tpp @@ -1,22 +1,25 @@ #ifndef _OTB_TRISKELE_AREA_ATTRIBUTES_TPP #define _OTB_TRISKELE_AREA_ATTRIBUTES_TPP -template inline -AreaAttributes::AreaAttributes (const Tree &tree, const WeightAttributes &weightAttributes, const unsigned int &treeCoreCount) - : CompAttributeC (tree, treeCoreCount) { - compute (weightAttributes); +AreaAttributes::AreaAttributes (const Tree &tree, const unsigned int &treeCoreCount) + : CompAttributeC (tree, treeCoreCount) { + compute (); } -template inline -AreaAttributes::~AreaAttributes () { +AreaAttributes::~AreaAttributes () { } -template inline void -AreaAttributes::compute (const WeightAttributes &weightAttributes) { - // XXX +AreaAttributes::compute () { + computeSameCompLevel ([this] (const DimImg &parentId) { + DimImg area = 0; + tree.forEachChildTI (parentId, [this, &area] (const bool &isLeaf, const DimImg &childId) { + area += isLeaf ? 1 : CompAttribute::values [childId]; + }); + CompAttribute::values [parentId] = area; + }); } #endif // _OTB_TRISKELE_AREA_ATTRIBUTES_TPP diff --git a/include/Attributes/WeightAttributes.hpp b/include/Attributes/WeightAttributes.hpp index 0e41e85..a4ef7af 100644 --- a/include/Attributes/WeightAttributes.hpp +++ b/include/Attributes/WeightAttributes.hpp @@ -13,6 +13,8 @@ namespace otb { public: inline WeightAttributes (const Tree &tree); inline ~WeightAttributes (); + + inline void setWeightBounds (Tree &tree); }; #include "WeightAttributes.tpp" diff --git a/include/Attributes/WeightAttributes.tpp b/include/Attributes/WeightAttributes.tpp index 100ccdb..f7bfd96 100644 --- a/include/Attributes/WeightAttributes.tpp +++ b/include/Attributes/WeightAttributes.tpp @@ -12,4 +12,31 @@ inline WeightAttributes::~WeightAttributes () { } +template +inline void +WeightAttributes::setWeightBounds (Tree &tree) { + vector &weightBounds (tree.getWeightBounds ()); + DimImg rootId = tree.getCompRoot (); + + DimImg stepsCard = 0; + WeightT curLevel = CompAttribute::values [0]; + for (DimImg compId = 1; compId <= rootId; ++compId) { + if (CompAttribute::values [compId] == curLevel) + continue; + ++stepsCard; + curLevel = CompAttribute::values [compId]; + } + weightBounds.clear (); + weightBounds.reserve (stepsCard+1); + weightBounds.push_back (0); + curLevel = CompAttribute::values [0]; + for (DimImg compId = 1; compId <= rootId; ++compId) { + if (CompAttribute::values [compId] == curLevel) + continue; + weightBounds.push_back (compId); + curLevel = CompAttribute::values [compId]; + } + weightBounds.push_back (rootId+1); +} + #endif // _OTB_TRISKELE_WEIGHT_ATTRIBUTES_TPP diff --git a/include/Tree.hpp b/include/Tree.hpp index 45604fd..7042670 100644 --- a/include/Tree.hpp +++ b/include/Tree.hpp @@ -30,13 +30,19 @@ namespace otb { Size size; /*! Number of pixels / leaf */ - DimNodeId leafCount, nodeCount; + DimImg leafCount; + /*! number of node */ + DimNodeId nodeCount; /* nodeCount = leafCount+compCount */ /*! Pointers on the parents of each leafs / nodes */ - DimNodeId *leafParents, *compParents; + DimImg *leafParents, *compParents; /*! Pointers on the children and count how many children a parents have */ - DimNodeId *children, *childCount; + DimImg *childCount; + DimNodeId *children; + + /*! Pointers of same weight in parents (+1 : last is root)*/ + vector weightBounds; /*! State of the tree */ State state; @@ -68,26 +74,34 @@ namespace otb { inline Size getSize () const; // Getters for tree structure - inline DimNodeId getRoot () const; - inline DimNodeId getAbsRoot () const; - inline DimNodeId getCompCount () const; + inline const DimImg &getLeafCount () const; + inline const DimNodeId &getNodeCount () const; + inline DimImg getCompCount () const; + inline DimNodeId getNodeRoot () const; + inline DimImg getCompRoot () const; - inline const DimNodeId &getParent (const DimNodeId &idx) const; - inline const DimNodeId &getLeafParent (const DimNodeId &idx) const; - inline const DimNodeId &getCompParent (const DimNodeId &idx) const; - inline const DimNodeId &getChildrenCount (const DimImg &idx) const; - inline const DimSideImg &getLeafCount () const; - inline const DimSideImg &getNodeCount () const; + inline const DimImg &getParent (const DimNodeId &nodeId) const; + inline const DimImg &getLeafParent (const DimImg &leafId) const; + inline const DimImg &getCompParent (const DimImg &compId) const; + + inline const DimImg &getChildrenCount (const DimImg &CompId) const; + inline const DimNodeId &getChildren (const DimImg &childId) const; + inline const DimNodeId *getChildren () const; // XXX a virer ? + + inline const vector &getWeightBounds () const; + inline vector &getWeightBounds (); // Functions to apply to specific entities template - void forEachLeaf (const FuncToApply &f /* f (DimNodeId leafId) */) const; + void forEachLeaf (const FuncToApply &f /* f (DimImg leafId) */) const; template - inline void forEachComp (const FuncToApply &f /* f (DimNodeId compId) */) const; + inline void forEachComp (const FuncToApply &f /* f (DimImg compId) */) const; template inline void forEachChild (const DimNodeId &parentId, const FuncToApply &f /* f (DimNodeId childId) */) const; + template + inline void forEachChildTI (const DimNodeId &parentId, const FuncToApply &f /* f (bool isLeaf, DimImg childId) */) const; #ifdef ENABLE_LOG // Print info about the tree diff --git a/include/Tree.tpp b/include/Tree.tpp index f596827..5d5ad2a 100644 --- a/include/Tree.tpp +++ b/include/Tree.tpp @@ -1,6 +1,7 @@ #ifndef _OTB_TRISKELE_TREE_TPP #define _OTB_TRISKELE_TREE_TPP +// ======================================== inline DimNodeId * Tree::getChildCount () { return childCount; @@ -25,56 +26,74 @@ Tree::getSize () const { return size; } -inline DimNodeId -Tree::getRoot () const { - return nodeCount-1; -} -inline DimNodeId -Tree::getAbsRoot () const { - return nodeCount-1+leafCount; -} -inline DimNodeId -Tree::getCompCount () const { - return nodeCount-leafCount; -} - -inline const DimNodeId & -Tree::getParent (const DimNodeId &idx) const { - return leafParents[idx]; -} -inline const DimNodeId & -Tree::getLeafParent (const DimNodeId &idx) const { - return leafParents[idx]; -} -inline const DimNodeId & -Tree::getCompParent (const DimNodeId &idx) const { - return compParents[idx]; -} -inline const DimNodeId & -Tree::getChildrenCount (const DimImg &idx) const { - return childCount[idx]; -} inline const DimSideImg & Tree::getLeafCount () const { return leafCount; } -inline const DimSideImg & +inline const DimNodeId & Tree::getNodeCount () const { return nodeCount; } +inline DimImg +Tree::getCompCount () const { + return (DimImg) (nodeCount-leafCount); +} +inline DimNodeId +Tree::getNodeRoot () const { + return nodeCount-1; +} +inline DimImg +Tree::getCompRoot () const { + return (DimImg) (nodeCount-1-leafCount); +} +inline const DimImg & +Tree::getParent (const DimNodeId &nodeId) const { + return leafParents[nodeId]; +} +inline const DimImg & +Tree::getLeafParent (const DimImg &leafId) const { + return leafParents[leafId]; +} +inline const DimImg & +Tree::getCompParent (const DimImg &compId) const { + return compParents[compId]; +} +inline const DimImg & +Tree::getChildrenCount (const DimImg &compId) const { + return childCount[compId]; +} +inline const DimNodeId & +Tree::getChildren (const DimImg &childId) const { + return children[childId]; +} +inline const DimNodeId * +Tree::getChildren () const { + return children; +} + +inline const vector & +Tree::getWeightBounds () const { + return weightBounds; +} +inline vector & +Tree::getWeightBounds () { + return weightBounds; +} + +// ======================================== template inline void -Tree::forEachLeaf (const FuncToApply &f /* f (DimNodeId leafId) */) const { - for (DimNodeId leafId = 0; leafId < leafCount; ++leafId) +Tree::forEachLeaf (const FuncToApply &f /* f (DimImg leafId) */) const { + for (DimImg leafId = 0; leafId < leafCount; ++leafId) f (leafId); } template inline void -Tree::forEachComp (const FuncToApply &f /* f (DimNodeId compId) */) const { - DimNodeId compCount = nodeCount - leafCount; +Tree::forEachComp (const FuncToApply &f /* f (DimImg compId) */) const { + const DimNodeId compCount = getCompCount (); for (DimNodeId compId = 0; compId < compCount; ++compId) f (compId); } @@ -86,5 +105,15 @@ Tree::forEachChild (const DimNodeId &parentId, const FuncToApply &f /* f (DimNod for (DimNodeId childId = minChild; childId < maxChild; ++childId) f (children[childId]); } +template +inline void +Tree::forEachChildTI (const DimNodeId &parentId, const FuncToApply &f /* f (bool isLeaf, DimImg childId) */) const { + DimNodeId minChild = childCount[parentId], maxChild = childCount[parentId+1]; + for (DimNodeId childId = minChild; childId < maxChild; ++childId) { + const DimNodeId &child (getChildren (childId)); + const bool isLeaf = child < leafCount; + f (isLeaf, isLeaf ? (DimImg) child : (DimImg) (child-leafCount)); + } +} #endif // _OTB_TRISKELE_TREE_TPP diff --git a/include/triskeleBase.tpp b/include/triskeleBase.tpp index e02accc..c3e56b9 100644 --- a/include/triskeleBase.tpp +++ b/include/triskeleBase.tpp @@ -108,9 +108,9 @@ idx2point (const Size &size, const DimImg &idx) { } -template +template inline std::ostream & -printMap (std::ostream &out, const DimImg *map, const Size &size, DimNodeId maxValues) { +printMap (std::ostream &out, const T *map, const Size &size, DimNodeId maxValues) { if (size.width > printMapMaxSide || size.height > printMapMaxSide) { return out << "map too big to print!" << std::endl; } @@ -121,9 +121,9 @@ printMap (std::ostream &out, const DimImg *map, const Size &size, DimNodeId maxV if (!maxValues) return out; if (*map == DimImg_MAX) - out << " M"; + out << " M "; else - out << std::setw(3) << *map; + out << std::setw(3) << ((double) *map) << " "; } out << std::endl; } diff --git a/src/Tree.cpp b/src/Tree.cpp index f04c3e2..4b2583e 100644 --- a/src/Tree.cpp +++ b/src/Tree.cpp @@ -38,6 +38,7 @@ Tree::clear () { fill_n (children, (leafCount-1)*2, 0); fill_n (childCount, leafCount+1, 0); #endif + weightBounds.resize (0); } void @@ -57,6 +58,7 @@ Tree::free () { if (childCount) delete[] childCount; children = childCount = nullptr; + weightBounds.resize (0); } void diff --git a/src/XMLTree/XMLTreeBuilder.cpp b/src/XMLTree/XMLTreeBuilder.cpp index 04ffe63..0c174ce 100644 --- a/src/XMLTree/XMLTreeBuilder.cpp +++ b/src/XMLTree/XMLTreeBuilder.cpp @@ -81,7 +81,7 @@ XMLTreeBuilder::exportToFile (const Tree &tree, const std::string &fileName) { treeNode->SetAttribute ("height", tree.getSize ().height); // Construct the tree - writeNodeChildren (tree, tree.getAbsRoot (), treeNode); + writeNodeChildren (tree, tree.getNodeRoot (), treeNode); doc.SaveFile (fileName.c_str ()); } diff --git a/src/testMain.cpp b/src/testMain.cpp index c514b8b..b3ea0bc 100644 --- a/src/testMain.cpp +++ b/src/testMain.cpp @@ -21,6 +21,9 @@ #include "Attributes/WeightAttributes.hpp" #include "AttributeProfiles.hpp" #include "Attributes/AreaAttributes.hpp" +#include "Attributes/AverageAttributes.hpp" +#include "Attributes/SDAttributes.hpp" +#include "Attributes/XYAttributes.hpp" //using namespace triskele; using namespace otb::triskele; @@ -32,25 +35,30 @@ void prog (const Option &option) { Raster raster; option.inputImage.readBand (raster, option.chanel, option.topLeft, option.size); - Border border; + Border border; // default = no border GraphWalker graphWalker (raster.getSize (), border); ArrayTreeBuilder atb (option.treeCoreCount, raster, graphWalker, TreeType::MAX); Tree tree; WeightAttributes weightAttributes (tree); atb.buildTree (tree, weightAttributes); + weightAttributes.printValues ("weight"); - // XXX buildAttr AttributeProfiles attributeProfiles (tree); atb.setAttributProfiles (attributeProfiles, raster); + attributeProfiles.printValues (); - AreaAttributes areaAttributes (tree, weightAttributes, option.treeCoreCount); - // AverageAttributs averageAttributs (tree, raster); - // SDAttributs sdAttributs (tree, areaAttributs); // XXX - // XYAttributs xyAttributs (tree, areaAttributs); // XXX - // MoIAttributs moiAttributs (tree, xyAttributs); // XXX + AreaAttributes areaAttributes (tree, option.treeCoreCount); + areaAttributes.printValues ("area"); + AverageAttributes averageAttributes (tree, raster, areaAttributes, option.treeCoreCount); + averageAttributes.printValues ("average"); + SDAttributes sdAttributes (tree, areaAttributes, option.treeCoreCount); + sdAttributes.printValues ("sd"); + XYAttributes xyAttributes (tree, areaAttributes, option.treeCoreCount); + //xyAttributes.printValues ("XY"); // pb print de complexe + // MoIAttributes moiAttributes (tree, xyAttributes); // XXX // XXX cut - // attribut.cut (out, attributProfiles, threshold) + // attribute.cut (out, attributProfiles, threshold) // XXX write