Implementation of classes that help to group and print information about found features and their parameters in Manufacturing Toolkit examples.
Overview
This helper is used to store, group and print found features after using one or more MTK analysis tools.
Implementation
FeatureGroup
class is used to store and sort MTK features with the same given name. Features are sorted using MTKBase_FeatureComparator class that based on comparison of features' type and parameters.
class FeatureGroup
{
public:
FeatureGroup (const std::string& theName, const std::string& theSubgroupName, bool theHasParameters) :
myName (theName), mySubgroupName (theSubgroupName), myHasParameters (theHasParameters)
{}
size_t FeatureCount() const
{
size_t aCount = 0;
for (const auto& aFeatureSubgroup : myFeatureSubgroups) {
aCount += aFeatureSubgroup.second;
}
return aCount;
}
string myName;
string mySubgroupName;
bool myHasParameters;
map<MTKBase_Feature, size_t, MTKBase_FeatureComparator> myFeatureSubgroups;
};
FeatureGroupComparator
class is used to sort FeatureGroup
entities before printing them into console. Sorting is based on group names and MTKBase_FeatureComparator class.
class FeatureGroupComparator
{
public:
bool operator() (const FeatureGroup& theA, const FeatureGroup& theB) const
{
const auto& anAName = theA.myName;
const auto& aBName = theB.myName;
if (anAName == aBName) {
return false;
}
const auto& anAFeatureSubgroups = theA.myFeatureSubgroups;
const auto& aBFeatureSubgroups = theB.myFeatureSubgroups;
if (anAFeatureSubgroups.empty() || aBFeatureSubgroups.empty()) {
return anAName < aBName;
}
return MTKBase_FeatureComparator() (anAFeatureSubgroups.begin()->first,
aBFeatureSubgroups.begin()->first);
}
};
FeatureGroupManager
class is used to create, store and update FeatureGroup
entities and printing them into a console. AddFeature
method is used to add MTKBase_Feature with given name to one of the existing FeatureGroup
entities or create a new one.
void AddFeature (const char* theGroupName,
const char* theSubgroupName,
bool theHasParameters,
const MTKBase_Feature& theFeature)
{
auto aRes = std::find_if (myGroups.begin(), myGroups.end(),
[&] (const FeatureGroup& theGroup) { return theGroup.myName == theGroupName; });
if (aRes == myGroups.end()) {
aRes = myGroups.insert (aRes, FeatureGroup (theGroupName, theSubgroupName, theHasParameters));
}
auto& aGroup = *aRes;
++aGroup.myFeatureSubgroups[theFeature];
}
Print
method is used to print with specific formatting all stored information. FeatureGroup
entities are additionally sorted before printing into console. User-defined function thePrintFeatureParameters
is used to print parameters of each stored MTKBase_Feature.
void Print (const char* theFeatureType, function<void (MTKBase_Feature)> thePrintFeatureParameters)
{
sort (myGroups.begin(), myGroups.end(), FeatureGroupComparator());
cout << setprecision(6);
size_t aTotalCount = 0;
for (const auto& aFeatureGroup : myGroups) {
size_t aFeatureCount = aFeatureGroup.FeatureCount();
aTotalCount += aFeatureCount;
cout << " " << aFeatureGroup.myName << ": " << aFeatureCount << endl;
if (!aFeatureGroup.myHasParameters) {
continue;
}
const char* aSubgroupName = aFeatureGroup.mySubgroupName.c_str();
for (const auto& aFeatureSubgroup : aFeatureGroup.myFeatureSubgroups) {
cout << " " << aFeatureSubgroup.second << " " << aSubgroupName << " with" << endl;
thePrintFeatureParameters (aFeatureSubgroup.first);
}
}
cout << "\n Total " << theFeatureType << ": " << aTotalCount << "\n" << endl;
}
PrintFeatureParameter
method is used as a help method to print MTKBase_Feature parameters with specific formatting.
template <typename T>
static void PrintFeatureParameter (const char* theName, const T& theValue, const char* theUnits)
{
cout << " " << theName << ": " << theValue << " " << theUnits << endl;
}
Files