static void processModel(const JSONNode & nd, MatModel& model) { bool nlayset = false; bool rangeset = false; bool lengthset = false; bool heightset = false; bool atomsset = false; for (auto i = nd.begin(); i != nd.end(); i++) { if (i->name() == JsonNames::numlayers) { model.mNumLayers = i->as_int(); nlayset = true; } else if (i->name() == JsonNames::range) { model.mRadius = i->as_float(); rangeset = true; } else if (i->name() == JsonNames::length) { model.mLength = i->as_float(); lengthset = true; } else if (i->name() == JsonNames::height) { model.mHeight = i->as_float(); heightset = true; } else if (i->name() == JsonNames::atoms) { BNB_ASSERT(nlayset); readIntVector(*i, model.mNumLayers, model.mLayersAtoms); atomsset = true; } else { BNB_ERROR_REPORT("Illegal name on parsing model data"); } } BNB_ASSERT(nlayset && rangeset && lengthset && heightset && atomsset); }
std::string to_string(Term* term) { caValue* value = term_value(term); bool valueHasAlpha = as_float(list_get(value,3)) < 1.0; int specifiedDigits = term->intProp("syntax:colorFormat", 6); int digitsPerChannel = (specifiedDigits == 6 || specifiedDigits == 8) ? 2 : 1; bool specifyAlpha = valueHasAlpha || (specifiedDigits == 4 || specifiedDigits == 8); std::stringstream out; out << "#"; for (int c=0; c < 4; c++) { if (c == 3 && !specifyAlpha) break; double channel = std::min((double) as_float(list_get(value, c)), 1.0); if (digitsPerChannel == 1) out << number_to_hex_digit(int(channel * 15.0)); else { int mod_255 = int(channel * 255.0); out << number_to_hex_digit(mod_255 / 0x10); out << number_to_hex_digit(mod_255 % 0x10); } } return out.str(); }
void Term__tweak(caStack* stack) { Term* t = as_term_ref(circa_input(stack, 0)); if (t == NULL) return circa_output_error(stack, "NULL reference"); int steps = tweak_round(to_float(circa_input(stack, 1))); caValue* val = term_value(t); if (steps == 0) return; if (is_float(val)) { float step = get_step(t); // Do the math like this so that rounding errors are not accumulated float new_value = (tweak_round(as_float(val) / step) + steps) * step; set_float(val, new_value); } else if (is_int(val)) set_int(val, as_int(val) + steps); else circa_output_error(stack, "Ref is not an int or number"); }
std::string Value::as_string() const { //if the data is stored as a string if(m_storedAs == valueType_string) { try { return any_cast<std::string>(m_value); } catch(std::bad_cast&) { throw Error_BadDataType(); } } //build the string depending on how the value is stored switch(m_storedAs) { case valueType_uint8: return Utils::toStr(static_cast<uint16>(as_uint8())); case valueType_uint16: return Utils::toStr(as_uint16()); case valueType_uint32: return Utils::toStr(as_uint32()); case valueType_int16: return Utils::toStr(as_int16()); case valueType_int32: return Utils::toStr(as_int32()); case valueType_float: return Utils::toStr(as_float()); case valueType_double: return Utils::toStr(as_double()); case valueType_bool: return Utils::toStr(as_bool()); default: throw Error_BadDataType(); } }
float Term::floatPropOptional(std::string const& name, float defaultValue) { caValue* value = term_get_property(this, name.c_str()); if (value == NULL) return defaultValue; else return as_float(value); }
float Term::floatProp(Symbol key, float defaultValue) { Value* value = term_get_property(this, key); if (value == NULL) return defaultValue; else return as_float(value); }
static void readDoubleVector(const JSONNode& nd, int vecsz, double * x) { int k = 0; for (auto i = nd.begin(); i != nd.end(); i++) { double u = i->as_float(); BNB_ASSERT(k < vecsz); x[k++] = u; } BNB_ASSERT(k == vecsz); }
int to_int(caValue* value) { if (is_int(value)) return as_int(value); else if (is_float(value)) return (int) as_float(value); internal_error("In to_float, type is not an int or float"); return 0; }
static void processLattice(const JSONNode & nd, int n, double& v, double* x) { for (auto i = nd.begin(); i != nd.end(); i++) { if (i->name() == JsonNames::evalue) { v = i->as_float(); } else if (i->name() == JsonNames::lvector) { readDoubleVector(*i, n, x); } else { BNB_ERROR_REPORT("Unknown name while processing lattice description"); } } }
static std::shared_ptr<MetadataStream::VideoSegment> parseVideoSegmentFromNode(JSONNode& segmentNode) { auto segmentTitleIter = segmentNode.find(ATTR_SEGMENT_TITLE); auto segmentFPSIter = segmentNode.find(ATTR_SEGMENT_FPS); auto segmentTimeIter = segmentNode.find(ATTR_SEGMENT_TIME); auto segmentDurationIter = segmentNode.find(ATTR_SEGMENT_DURATION); auto segmentWidthIter = segmentNode.find(ATTR_SEGMENT_WIDTH); auto segmentHeightIter = segmentNode.find(ATTR_SEGMENT_HEIGHT); if(segmentTitleIter == segmentNode.end()) VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has no title"); if(segmentFPSIter == segmentNode.end()) VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has no fps value"); if(segmentTimeIter == segmentNode.end()) VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has no time value"); std::string title = segmentTitleIter->as_string(); double fps = segmentFPSIter->as_float(); long long timestamp = segmentTimeIter->as_int(); if(title.empty()) VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has invalid title"); if(fps <= 0) VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has invalid fps value"); if(timestamp < 0) VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has invalid time value"); std::shared_ptr<MetadataStream::VideoSegment> spSegment(new MetadataStream::VideoSegment(title, fps, timestamp)); if(segmentDurationIter != segmentNode.end()) { long long duration = segmentDurationIter->as_int(); if(duration > 0) spSegment->setDuration(duration); } if(segmentWidthIter != segmentNode.end() && segmentHeightIter != segmentNode.end()) { long width = segmentWidthIter->as_int(), height = segmentHeightIter->as_int(); if(width > 0 && height > 0) spSegment->setResolution(width, height); } return spSegment; }
bool Value::isSameValue(const Value& other) const { switch(m_storedAs) { case valueType_float: return as_float() == other.as_float(); case valueType_double: return as_double() == other.as_double(); case valueType_uint16: return as_uint16() == other.as_uint16(); case valueType_uint32: return as_uint32() == other.as_uint32(); case valueType_int16: return as_int16() == other.as_int16(); case valueType_int32: return as_int32() == other.as_int32(); case valueType_uint8: return as_uint8() == other.as_uint8(); case valueType_bool: return as_bool() == other.as_bool(); case valueType_string: return as_string() == other.as_string(); default: return false; } }
operator float() const { return as_float(); }
float Term::floatProp(std::string const& name) { caValue* t = addProperty(name, FLOAT_TYPE); return as_float(t); }
const char* list_as_string( BehaviorTreeContext tree, Parameter* v ) { const char* ret = ""; int space = 0; int written = 0; char* str = 0x0; char* s; while( v ) { char tmp[1024]; int n = sprintf( tmp, "%s = ", v->m_Id.m_Text ); if( space - (n + written + 1) < 0 ) { space = max( space * 2, n + written + 1 ); str = (char*)realloc( str, space ); } s = str + written; memcpy( s, tmp, n + 1 ); written += n; switch( v->m_Type ) { case E_VART_INTEGER: n = sprintf( tmp, "%d", as_integer( *v ) ); break; case E_VART_FLOAT: n = sprintf( tmp, "%f", as_float( *v ) ); break; case E_VART_STRING: n = sprintf( tmp, "\"%s\"", as_string( *v )->m_Parsed ); break; case E_VART_BOOL: n = sprintf( tmp, "%s", as_bool( *v ) ? "true" : "false" ); break; case E_VART_HASH: n = sprintf( tmp, "0x%08x", as_hash( *v ) ); break; case E_VART_UNDEFINED: case E_MAX_VARIABLE_TYPE: break; } v = v->m_Next; if( v ) { tmp[++n] = ','; tmp[++n] = ' '; tmp[n + 1] = 0; } if( space - (n + written + 1) < 0 ) { space = max( space * 2, n + written + 1 ); str = (char*)realloc( str, space ); } s = str + written; memcpy( s, tmp, n + 1 ); written += n; } if( str ) { if( tree ) ret = register_string( tree, str ); free( str ); str = 0x0; } return ret; }
void TransformEnt::LoadFromXml(xml_node& xml, Setting& defaultSettings) { this->name = xml.attribute("name").as_string(); // calculation type is LOCAL by default pType = sType = CALCTYPE_LOC; // =================== get positions auto posAttrib = xml.attribute("pos"); if (posAttrib) { string posString = posAttrib.as_string(); float posF = defaultSettings.GetItemValFloat("pos", 0.0); pType = EnumConverter::GetUnitValue(posString, posF); pos = ofVec2f(posF, posF); } else { string posXStr = xml.attribute("pos_x").as_string(); string posYStr = xml.attribute("pos_y").as_string(); float posX = defaultSettings.GetItemValFloat("pos_x", 0.0); float posY = defaultSettings.GetItemValFloat("pos_y", 0.0); if (!posXStr.empty()) pType = EnumConverter::GetUnitValue(posXStr, posX); if (!posYStr.empty()) pType = EnumConverter::GetUnitValue(posYStr, posY); pos = ofVec2f(posX, posY); } zIndex = xml.attribute("z_index").as_int(defaultSettings.GetItemValFloat("z_index", 0)); // =================== get size float width = 0; float height = 0; auto sizeAttr = xml.attribute("size"); if (sizeAttr) { string sizeStr = sizeAttr.as_string("1.0"); float size = defaultSettings.GetItemValFloat("size", 1.0); sType = EnumConverter::GetUnitValue(sizeStr, size); width = height = size; } else { string widthStr = xml.attribute("width").as_string(); string heightStr = xml.attribute("height").as_string(); width = defaultSettings.GetItemValFloat("width", 0.0); height = defaultSettings.GetItemValFloat("height", 0.0); // calc width and height if (!widthStr.empty()) sType = EnumConverter::GetUnitValue(widthStr, width); // 0 means that the other value will be taken from aspect ratio multiplied by the width if (!heightStr.empty()) { if (heightStr.compare("0") != 0) { // calculate scaleType from height (it may be different) sType = EnumConverter::GetUnitValue(heightStr, height); } else { height = 0; } } // set the other property to 0 so that we know that it should have the same value if (width != height) { if (width != 0 && height == 1) height = 0; else if (height != 0 && width == 1) width = 0; } } size = ofVec2f(width, height); // =================== get anchor auto anchorAttr = xml.attribute("anchor"); if (anchorAttr) { float anchorFlt = anchorAttr.as_float(0.0f); anchor = ofVec2f(anchorFlt); } else { anchor.x = xml.attribute("anchor_x").as_float(defaultSettings.GetItemValFloat("anchor_x", 0.0)); anchor.y = xml.attribute("anchor_y").as_float(defaultSettings.GetItemValFloat("anchor_y", 0.0)); } // get rotation and centroid rotation = xml.attribute("rotation").as_float(defaultSettings.GetItemValFloat("rotation", 0.0)); auto rotOrigAttr = xml.attribute("rotation_origin"); if (rotOrigAttr) { float rotOrigin = rotOrigAttr.as_float(0.5f); rotationCentroid = ofVec2f(rotOrigin); } else { rotationCentroid.x = xml.attribute("rotation_origin_x").as_float(defaultSettings.GetItemValFloat("rotation_origin_x", 0.0)); rotationCentroid.y = xml.attribute("rotation_origin_y").as_float(defaultSettings.GetItemValFloat("rotation_origin_y", 0.0)); } }
TEST_FIXTURE( VariableTypeBoolFixture, BoolConvertsCorrectlyToFloat ) { CHECK( 1.0f == as_float( v ) ); }
TEST_FIXTURE( VariableTypeStringFixture, StringConvertsToZeroFloat ) { CHECK( 0.0f == as_float( v ) ); }
TEST_FIXTURE( VariableTypeIntegerFixture, IntegerToFloatConversionDoesNotReturnIncorrectValue ) { v.m_Data.m_Integer = 17; CHECK( 17.0002f != as_float( v ) ); }
TEST_FIXTURE( VariableTypeIntegerFixture, IntegerToFloatConversionReturnsCorrectValue ) { v.m_Data.m_Integer = 17; CHECK( 17.0f == as_float( v ) ); }