static void copyPlane(const T * srcp, float * dstp, const int width, const int height, const int srcStride, const int dstStride, const float offset) noexcept { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x += 8) { if (std::is_same<T, uint8_t>::value) to_float(Vec8i().load_8uc(srcp + x)).stream(dstp + x); else if (std::is_same<T, uint16_t>::value) to_float(Vec8i().load_8us(srcp + x)).stream(dstp + x); else (Vec8f().load_a(srcp + x) + offset).stream(dstp + x); } srcp += srcStride; dstp += dstStride; } }
void LayoutEngine::getGlyphPosition(le_int32 glyphIndex, float &x, float &y, LEErrorCode &success) const { if (LE_FAILURE (success)) return; unsigned int count; const hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (fHbBuffer, &count); unsigned int i; x = this->x; y = this->y; for (i = 0; i < (unsigned int) glyphIndex; i++) { x += to_float (pos[i].x_advance); y += to_float (pos[i].y_advance); } x += to_float (pos[glyphIndex].x_offset); y += to_float (pos[glyphIndex].y_offset); }
void LayoutEngine::getGlyphPositions(float positions[], LEErrorCode &success) const { if (LE_FAILURE (success)) return; unsigned int count; const hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (fHbBuffer, &count); float x = this->x, y = this->y; unsigned int i; for (i = 0; i < count; i++) { positions[2 * i] = x + to_float (pos[i].x_offset); positions[2 * i + 1] = y + to_float (pos[i].y_offset); x += to_float (pos[i].x_advance); y += to_float (pos[i].y_advance); } positions[2 * i] = x; positions[2 * i + 1] = y; }
void _tok::round(void) { double value = 0.0; std::stringstream ss; if(_class_type != CLASS_TOKEN_TYPE_TOKEN) { THROW_TOK_EXC_W_MESS(_class_type, TOK_EXC_INVALID_CLS_TYPE); } if(_type == TOKEN_TYPE_INTEGER) { return; } switch(_type) { case TOKEN_TYPE_FLOAT: value = std::floor(to_float() + 0.5); ss << (double) value; break; case TOKEN_TYPE_INTEGER: break; default: THROW_TOK_EXC_W_MESS(_type, TOK_EXC_INVALID_TYPE); break; } _f_val = value; _i_val = (long) value; _s_val = ss.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"); }
int do_train(string arg) { object ob; object pet_ob; string pet_id; object gold; int cost; string which; ob = this_player(); if(ob->is_busy()) return notify_fail("你正忙着呢。\n"); if (!ob->query("pet/pet_id")) return notify_fail("小马官道:"你并没有坐骑,来凑什么热闹?"\n"); pet_id=ob->query("pet/pet_id"); if( !objectp(pet_ob=present(pet_id, ob)) && !objectp(pet_ob=present(pet_id, environment(ob)))) return notify_fail("小马官道:"你的坐骑没和你一起来吗?"\n"); if(pet_ob->query("owner") != ob->query("id")) return notify_fail("小马官道:"那好象不是你的吧?"\n"); switch (random(2)){ case 0: which = "combat_exp"; break; case 1: which = "daoxing"; break; } //好,我们现在花钱来增加坐骑的wx, dh cost = to_int(sqrt(to_float(((int)pet_ob->query("combat_exp")+(int)pet_ob->query("combat_exp")+100))))/5; gold = present("gold_money", ob); if(!gold) return notify_fail("你身上没有金子。\n"); if((int) gold->query_amount() < cost) return notify_fail("你身上没有"+chinese_number(cost)+"两金子。\n"); pet_ob->add(which,cost*50); pet_ob->save(); gold->add_amount(-cost); command("say 好了!下次再打!\n"); return 1; }
int do_train(string arg) { object me,pet,gold; int cost; me = this_player(); if(me->is_busy()) return notify_fail("你上一个动作还没有完成。\n"); if(!arg) return notify_fail("王虎道:"你要什么呀?"\n"); pet = present(arg,environment()); if(!objectp(pet)) return notify_fail("王虎道:"你要什么呀?"\n"); if(pet->query("possessed") != me ) return notify_fail("王虎道:"那好象不是你的吧?"\n"); //okey we identified the target, now the cost: cost = to_int(sqrt(to_float((int)pet->query("combat_exp"))))/5; gold = present("gold_money", me); if(!gold) return notify_fail("你身上没有金子。\n"); if((int) gold->query_amount() < cost) return notify_fail("你身上没有"+chinese_number(cost)+"两金子。\n"); if((int)me->query("score") < cost) return notify_fail("你的评价不够"+chinese_number(cost)+"点。\n"); pet->add("combat_exp",cost*50); pet->save(); gold->add_amount(-cost); me->add("score",-cost); command("say 好了!下次再打!\n"); return 1; }
void Term__asfloat(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL term"); set_float(vm->output(), to_float(term_value(t))); }
int main() { init_genrand(0); uint32_t ain[100]; hls::stream<uint32_t> din; hls::stream<float> dout; for (int i = 0; i < 100; ++i) { uint32_t rn = genrand_int32();; ain[i] = rn; din.write(rn); } float aout[100]; for (int i = 0; i < 100/2; ++i) { float u1 = to_float(ain[2 * i]); float u2 = to_float(ain[2 * i + 1]); box_muller(u1, u2); aout[2 * i] = u1; aout[2 * i + 1] = u2; } gauss_transform(din, dout); if (dout.size() != 100) { std::cout << "wrong size" << std::endl; return -1; } for (int i = 0; i < 100; ++i) { float hw = dout.read(); float sw = aout[i]; std::cout << i << ": \t" << hw << " \t(hw) - (sw) " << sw << std::endl; if (!(std::isfinite(hw) and std::isfinite(sw) and std::abs(hw - sw) < 1E-4)) { std::cout << "ERROR: " << i << ": \t" << hw << " \t(hw|sw) " << sw << std::endl; return -1; } } std::cout << "passed." << std::endl; return 0; }
int wdt_fetch_value(struct debuggee* dbg, struct mval* mv) { int ret = 0; if (!dbg->cl.buf_ptr[0]) { mv->type = mv_null; } else if (compare(re_float1, dbg->cl.buf_ptr) || compare(re_float2, dbg->cl.buf_ptr) || compare(re_float3, dbg->cl.buf_ptr)) { mv->type = mv_float; mv->u.flt_number = to_float(dbg, 1); } else if (compare(re_integer, dbg->cl.buf_ptr)) { mv->type = mv_integer; mv->u.integer = to_num(dbg, 1); } else if (compare(re_hexa, dbg->cl.buf_ptr)) { mv->type = mv_hexa; mv->u.integer = to_num(dbg, 1); } else if (compare(re_string, dbg->cl.buf_ptr)) { mv->type = mv_string; mv->u.str = to_string(dbg, 1); } else if (compare(re_char, dbg->cl.buf_ptr)) { mv->type = mv_char; mv->u.integer = dbg->cl.buf_ptr[rm[1].rm_so]; } else if (compare(re_struct, dbg->cl.buf_ptr)) { mv->type = mv_struct; mv->u.str = to_string(dbg, 1); } else if (compare(re_func, dbg->cl.buf_ptr)) { mv->type = mv_func; mv->u.integer = to_num(dbg, 1); } else if (compare(re_id, dbg->cl.buf_ptr)) { mv->type = mv_string; mv->u.str = to_string(dbg, 1); } else { mv->type = mv_error; ret = -1; } return ret; }
void Term__asfloat(caStack* stack) { Term* t = as_term_ref(circa_input(stack, 0)); if (t == NULL) { circa_output_error(stack, "NULL reference"); return; } set_float(circa_output(stack, 0), to_float(term_value(t))); }
static void gaussianBlurV(const T * __srcp, float * dstp, const float * weights, const int width, const int height, const int srcStride, const int dstStride, const int radius, const float offset) noexcept { const int diameter = radius * 2 + 1; const T ** _srcp = new const T *[diameter]; _srcp[radius] = __srcp; for (int i = 1; i <= radius; i++) { _srcp[radius - i] = _srcp[radius - 1 + i]; _srcp[radius + i] = _srcp[radius] + srcStride * i; } for (int y = 0; y < height; y++) { for (int x = 0; x < width; x += 4) { Vec4f sum = zero_4f(); for (int i = 0; i < diameter; i++) { if (std::is_same<T, uint8_t>::value) { const Vec4f srcp = to_float(Vec4i().load_4uc(_srcp[i] + x)); sum = mul_add(srcp, weights[i], sum); } else if (std::is_same<T, uint16_t>::value) { const Vec4f srcp = to_float(Vec4i().load_4us(_srcp[i] + x)); sum = mul_add(srcp, weights[i], sum); } else { const Vec4f srcp = Vec4f().load_a(_srcp[i] + x); sum = mul_add(srcp + offset, weights[i], sum); } } sum.stream(dstp + x); } for (int i = 0; i < diameter - 1; i++) _srcp[i] = _srcp[i + 1]; if (y < height - 1 - radius) _srcp[diameter - 1] += srcStride; else if (y > height - 1 - radius) _srcp[diameter - 1] -= srcStride; dstp += dstStride; } delete[] _srcp; }
double AVIWrapper::getAudioTime() { if (time_start == 0) { frame_start = (v_stream) ? v_stream->GetTime() : 0.; time_start = longcount(); } if (a_stream) return a_stream->GetTime(); else return frame_start + to_float(longcount(), time_start); }
bool operator== (const variant& rhs) const { if (type() == TYPE_NULL || rhs.type() == TYPE_NULL || type() != rhs.type()) return false; switch (type()) { case TYPE_BOOL: return to_bool() == rhs.to_bool(); case TYPE_INT: return to_int() == rhs.to_int(); case TYPE_DOUBLE: return to_float() == rhs.to_float(); case TYPE_STRING: return to_string() == rhs.to_string(); default: throw_type_error(type()); return false; // just to pease the compiler } }
void average(caStack* stack) { caValue* args = circa_input(stack, 0); int count = circa_count(args); caValue* out = circa_output(stack, 0); if (count == 0) { set_float(out, 0); return; } float sum = 0; for (int i=0; i < count; i++) sum += to_float(circa_index(args, i)); set_float(out, sum / count); }
int cmd_akkarsa(string str) { object tp=this_player(); int wis, summ, faith; int cost, len; int bad=0; float amt; if (!spell()) return 0; if (tp->query_disable()) return 1; if (!ok_cast(tp)) return 0; wis=tp->query_stats("wisdom"); summ=tp->query_skill("summoning"); faith=tp->query_skill("faith"); amt=0.5+to_float((summ-210)/600); if (amt > 0.7) amt=0.7; len=summ/22+faith/26+wis*3/2; len=len*3/5; cost=len*4+50; if (tp->query_mp() < cost) FAIL("Too low on magic.\n"); tp->set_disable(1); tp->add_mp(-cost); message("magic",COL+"You chant the word %^BOLD%^%^RED%^akkarsa"+RES+COL+".",tp); message("magic",COL+tp->query_cap_name()+" chants something."+RES,environment(tp),tp); if (tp->query_alignment() > -800) { message("magic","A horrible feeling washes over you.",tp); bad=1; } call_out("summon",2,({tp,amt,len,bad}));
void _tok::negate(void) { double value = 0.0; std::stringstream ss; if(_class_type != CLASS_TOKEN_TYPE_TOKEN) { THROW_TOK_EXC_W_MESS(_class_type, TOK_EXC_INVALID_CLS_TYPE); } switch(_type) { case TOKEN_TYPE_FLOAT: case TOKEN_TYPE_INTEGER: value = to_float() * -1.0; ss << (double) value; break; default: THROW_TOK_EXC_W_MESS(_type, TOK_EXC_INVALID_TYPE); break; } _f_val = value; _i_val = (long) value; _s_val = ss.str(); }
float from_string(const string & input_value) throw(invalid_argument, out_of_range) { return to_float(input_value); }
void create() { int skill, nl, lvl; mixed exp; NPC_D->generate_cn_name(this_object()); set("gender", "男性"); set("long", "这是一名大宋将军,正威风凛凛地巡视着自己的队伍。\n"); set("int", 30); set("str", 30 + random(20)); set("con", 20 + random(20)); set("dex", 20 + random(20)); set("age", 20 + random(30)); set("per", 15 + random(15)); set("age", 20 + random(30)); set("per", 15 + random(15)); set("no_get",1); exp = UPDATE_D->query_max_exp(); exp = count_div(exp, 10); skill = to_int(pow(to_float(atoi(count_div(exp, 100))), 1.0 / 3)) * 10; if (skill < 100) skill = 100; nl = skill * 10 + atoi(count_div(exp, 10000)); set("combat_exp", exp); set("max_neili", nl); set("neili", query("max_neili") * 3 / 2); set("max_jingli", nl); set("jingli", query("max_jingli")); set("max_qi", nl * 5); set("qi", query("max_qi")); set("eff_qi", query("max_qi")); set("max_jing", nl); set("eff_jing", query("max_jing")); set("jiali", skill / 2); set_skill("taiji-shengong", skill); set_skill("wuhu-duanmendao", skill); set_skill("tiyunzong", skill); set_skill("taiji-quan", skill); set_skill("cuff", skill); set_skill("force", skill); set_skill("dodge", skill); set_skill("blade", skill); set_skill("parry", skill); set_skill("arrow", skill*2/3); map_skill("force", "taiji-shengong"); map_skill("dodge", "tiyunzong"); map_skill("cuff", "taiji-quan"); map_skill("parry", "taiji-quan"); map_skill("blade", "wuhu-duanmendao"); prepare_skill("cuff", "taiji-quan"); set_temp("warquest/party", "song"); add_temp("apply/armor", 400); add_temp("apply/damage", 100); setup(); lvl = (query("str") + skill / 10) / 10 - 1; if (lvl > 8) lvl = 8; lvl = lvl - 2; if (lvl < 3) lvl = 3; carry_object("/clone/weapon/bow" + lvl); carry_object("/clone/weapon/bow" + lvl); carry_object("/clone/weapon/bow" + lvl); carry_object("/clone/weapon/bow" + lvl); carry_object(__DIR__"obj/wandao"); carry_object(__DIR__"obj/wandao"); carry_object(__DIR__"obj/wandao")->wield(); carry_object(__DIR__"obj/tiejia")->wear(); carry_object(__DIR__"obj/pifeng")->wear(); }
void test_equals_function(caValue* a, float b, const char* aText, const char* bText, int line, const char* file) { return test_equals_function(to_float(a), b, aText, bText, line, file); }
void to_expected(GLenum test_format, GLenum test_type, void *up_raw, float *expected) { float up_rgba[4]; int num_chan = num_channels(test_format); to_float(up_raw, num_chan, test_type, up_rgba); expected[0] = 0.0f; expected[1] = 0.0f; expected[2] = 0.0f; expected[3] = 1.0f; switch (test_format) { case GL_RED: expected[0] = up_rgba[0]; break; case GL_GREEN: expected[1] = up_rgba[0]; break; case GL_BLUE: expected[2] = up_rgba[0]; break; case GL_ALPHA: expected[3] = up_rgba[0]; break; case GL_RG: expected[0] = up_rgba[0]; expected[1] = up_rgba[1]; break; case GL_RGBA: expected[3] = up_rgba[3]; case GL_RGB: expected[0] = up_rgba[0]; expected[1] = up_rgba[1]; expected[2] = up_rgba[2]; break; case GL_BGRA: expected[3] = up_rgba[3]; case GL_BGR: expected[0] = up_rgba[2]; expected[1] = up_rgba[1]; expected[2] = up_rgba[0]; break; case GL_ABGR_EXT: expected[0] = up_rgba[3]; expected[1] = up_rgba[2]; expected[2] = up_rgba[1]; expected[3] = up_rgba[0]; break; case GL_INTENSITY: expected[0] = up_rgba[0]; expected[1] = up_rgba[0]; expected[2] = up_rgba[0]; expected[3] = up_rgba[0]; break; case GL_LUMINANCE_ALPHA: expected[3] = up_rgba[1]; case GL_LUMINANCE: expected[0] = up_rgba[0]; expected[1] = up_rgba[0]; expected[2] = up_rgba[0]; break; default: assert(!"Invalid color format"); } switch (format->format) { case GL_RED: case GL_RED_INTEGER: expected[1] = 0.0f; case GL_RG: case GL_RG_INTEGER: expected[2] = 0.0f; case GL_RGB: case GL_RGB_INTEGER: expected[3] = 1.0f; break; case GL_RGBA: case GL_RGBA_INTEGER: break; case GL_ALPHA: expected[0] = 0.0f; expected[1] = 0.0f; expected[2] = 0.0f; break; case GL_LUMINANCE: expected[3] = 1.0f; case GL_LUMINANCE_ALPHA: expected[0] = expected[0]; expected[1] = expected[0]; expected[2] = expected[0]; break; default: assert(!"Invalid color format"); } if (!is_format_signed(format->internal_format)) { if (expected[0] < 0.0f) expected[0] = 0.0f; if (expected[1] < 0.0f) expected[1] = 0.0f; if (expected[2] < 0.0f) expected[2] = 0.0f; if (expected[3] < 0.0f) expected[3] = 0.0f; } if (is_format_srgb(format->internal_format)) { expected[0] = srgb_to_linear(expected[0]); expected[1] = srgb_to_linear(expected[1]); expected[2] = srgb_to_linear(expected[2]); } }
void set_owner(object tp) { set_short("A ghostly image of "+tp->query_cap_name()); set_long("This spirit looks very similar to "+tp->query_cap_name()+ ", except it is partly transparent."); set_name("ghostly image"); set_id(({"thousand-nights-ghost-image","ghostly image","ghost", "ghost image","image",tp->query_name()})); set_gender(tp->query_gender()); set_level(tp->query_level()/3); set_body_type(tp->query_body_type()); set_alignment(0); set_hp(query_max_hp()); set_sp(query_max_sp()); set_mp(query_max_mp()); owner=tp; set_exp_share(owner, to_float(1/2)); } void create() { ::create(); set_property("no_corpse",1); set_race("undead"); set_die("The ghostly image slowly fades away."); } void heart_beat() { object att; int i; ::heart_beat(); if (!owner) remove(); if (!present(owner->query_cap_name(),environment())) { if (!present("thousand-nights-ghost-image",environment(owner))) {
void MaterialParser::parse(std::istream& in) { bool default_program = true; bool has_diffuse_texture = false; bool has_specular_texture = false; bool has_reflection_texture = false; int current_texture_unit = 0; std::string program_vertex = "src/glsl/default.vert"; std::string program_fragment = "src/glsl/default.frag"; std::vector<std::string> program_vertex_defines; std::vector<std::string> program_fragment_defines; m_material->enable(GL_CULL_FACE); m_material->enable(GL_DEPTH_TEST); m_material->set_uniform("material.ambient", glm::vec3(1.0f, 1.0f, 1.0f)); int line_number = 0; std::string line; while(std::getline(in, line)) { try { line_number += 1; std::vector<std::string> args = argument_parse(line); if (!args.empty()) { if (args[0] == "material.diffuse") { m_material->set_uniform("material.diffuse", to_vec3(args.begin()+1, args.end(), glm::vec3(1.0f, 1.0f, 1.0f))); } else if (args[0] == "material.diffuse_texture") { has_diffuse_texture = true; if (args.size() == 2) { std::string diffuse_texture_name = to_string(args.begin()+1, args.end()); if (diffuse_texture_name == "buildin://video-texture") { m_material->set_video_texture(current_texture_unit); } else { m_material->set_texture(current_texture_unit, Texture::from_file(diffuse_texture_name)); } } else if (args.size() == 3) { m_material->set_texture(current_texture_unit, Texture::from_file(args[1]), Texture::from_file(args[2])); } else { throw std::runtime_error("broken"); } m_material->set_uniform("material.diffuse_texture", current_texture_unit); current_texture_unit += 1; } else if (args[0] == "material.specular") { m_material->set_uniform("material.specular", to_vec3(args.begin()+1, args.end(), glm::vec3(1.0f, 1.0f, 1.0f))); } else if (args[0] == "material.specular_texture") { has_specular_texture = true; m_material->set_texture(current_texture_unit, Texture::from_file(to_string(args.begin()+1, args.end()))); m_material->set_uniform("material.specular_texture", current_texture_unit); current_texture_unit += 1; } else if (args[0] == "material.shininess") { m_material->set_uniform("material.shininess", to_float(args.begin()+1, args.end())); } else if (args[0] == "material.reflection_texture") { has_reflection_texture = true; m_material->set_texture(current_texture_unit, Texture::cubemap_from_file(to_string(args.begin()+1, args.end()))); m_material->set_uniform("material.reflection_texture", current_texture_unit); current_texture_unit += 1; } else if (args[0] == "material.ambient") { m_material->set_uniform("material.ambient", to_vec3(args.begin()+1, args.end(), glm::vec3(1.0f, 1.0f, 1.0f))); } else if (args[0] == "blend_mode") { //m_material->set_ } else if (args[0] == "program.vertex") { program_vertex = args[1]; if (args.size() > 2) { program_vertex_defines.insert(program_vertex_defines.end(), args.begin() + 2, args.end()); } default_program = false; } else if (args[0] == "program.fragment") { program_fragment = args[1]; if (args.size() > 2) { program_fragment_defines.insert(program_fragment_defines.end(), args.begin() + 2, args.end()); } default_program = false; } else if (args[0] == "material.disable") { if (args[1] == "cull_face") { m_material->disable(GL_CULL_FACE); } else { throw std::runtime_error("unknown token: " + args[1]); } } else if (boost::algorithm::starts_with(args[0], "uniform.")) { std::string uniform_name = args[0].substr(8); int count = args.end() - args.begin() - 1; if (count == 3) { m_material->set_uniform(uniform_name, to_vec3(args.begin()+1, args.end(), glm::vec3(1.0f, 1.0f, 1.0f))); } else if (count == 1) { m_material->set_uniform(uniform_name, to_float(args.begin()+1, args.end())); } else { throw std::runtime_error("unknown argument count: " + args[0]); } } else { throw std::runtime_error("unknown token: " + args[0]); } } } catch(const std::exception& err) { throw std::runtime_error(format("%s:%d: error: %s at line:\n%s", m_filename, line_number, err.what(), line)); } } if (default_program) { if (has_diffuse_texture) { program_fragment_defines.emplace_back("DIFFUSE_COLOR_FROM_TEXTURE"); } else { program_fragment_defines.emplace_back("DIFFUSE_COLOR_FROM_MATERIAL"); } if (has_specular_texture) { program_fragment_defines.emplace_back("SPECULAR_COLOR_FROM_TEXTURE"); } else { program_fragment_defines.emplace_back("SPECULAR_COLOR_FROM_MATERIAL"); } if (has_reflection_texture) { program_fragment_defines.emplace_back("REFLECTION_TEXTURE"); } program_fragment_defines.emplace_back("SHADOW_VALUE_4"); } ProgramPtr program = Program::create(Shader::from_file(GL_VERTEX_SHADER, program_vertex, program_vertex_defines), Shader::from_file(GL_FRAGMENT_SHADER, program_fragment, program_fragment_defines)); m_material->set_program(program); }
{} data( const XERCES_CPP_NAMESPACE::DOMNode& parent, const XERCES_CPP_NAMESPACE::DOMNode& node ) : parent_( &parent ) , node_ ( &node ) {} //@} //! @name Operations //@{ void to( std::string& v ) const { XEUMEULEU_TRY if( node_ ) v = translate( get_data() ); XEUMEULEU_CATCH } void to( bool& v ) const { XEUMEULEU_TRY if( node_ ) v = to_bool( get_data() ); XEUMEULEU_CATCH } void to( short& v ) const { XEUMEULEU_TRY if( node_ ) v = convert< short >( get_data() ); XEUMEULEU_CATCH } void to( int& v ) const { XEUMEULEU_TRY if( node_ ) v = to_int( get_data() ); XEUMEULEU_CATCH } void to( long& v ) const { XEUMEULEU_TRY if( node_ ) v = convert< long >( get_data() ); XEUMEULEU_CATCH } void to( long long& v ) const { XEUMEULEU_TRY if( node_ ) v = convert< long long >( get_data() ); XEUMEULEU_CATCH } void to( float& v ) const { XEUMEULEU_TRY if( node_ ) v = to_float( get_data() ); XEUMEULEU_CATCH } void to( double& v ) const { XEUMEULEU_TRY if( node_ ) v = to_double( get_data() ); XEUMEULEU_CATCH } void to( long double& v ) const { XEUMEULEU_TRY if( node_ ) v = convert< long double >( get_data() ); XEUMEULEU_CATCH } void to( unsigned short& v ) const { XEUMEULEU_TRY if( node_ ) v = convert< unsigned short >( get_data() ); XEUMEULEU_CATCH } void to( unsigned int& v ) const { XEUMEULEU_TRY if( node_ ) v = convert< unsigned int >( get_data() ); XEUMEULEU_CATCH } void to( unsigned long& v ) const { XEUMEULEU_TRY if( node_ ) v = convert< unsigned long >( get_data() ); XEUMEULEU_CATCH } void to( unsigned long long& v ) const { XEUMEULEU_TRY if( node_ ) v = convert< unsigned long long >( get_data() ); XEUMEULEU_CATCH } //@} private: //! @name Helpers //@{ const XMLCh* get_data() const { return node_->getNodeValue(); }
void parse_world(state_t* state, FILE* f) { char line[4096]; char** tokens; int n_tokens; int bsdf_index = 0; transform_data_t t; t.has_users = false; t.index = 0; matrix_t m_identity = {{1.f,0.f,0.f,0.f}, {0.f,1.f,0.f,0.f}, {0.f,0.f,1.f,0.f}, {0.f,0.f,0.f,1.f}}; memcpy(t.t.m, m_identity, sizeof(matrix_t)); memcpy(t.t.m_inv, m_identity, sizeof(matrix_t)); state->transforms[t.index++] = t.t; state->n_transforms++; realloc_transforms(); transform_data_t tstack[1024]; int tstack_ptr = 0; while(fgets(line, 4096, f) != NULL) { if(line[0] != '#') { line[strlen(line)-1] = '\0'; tokens = tokenize(line, &n_tokens); if(n_tokens != 0) { if(strcmp(tokens[0],"EndWorld") == 0) { free(tokens); return; } else if(strcmp(tokens[0],"PushTransform") == 0) { qr_assert(n_tokens==1, "parser", "PushTransform takes only no arguments, found %d",n_tokens-1); tstack[++tstack_ptr] = t; } else if(strcmp(tokens[0],"PopTransform") == 0) { qr_assert(n_tokens==1, "parser", "PopTransform takes no arguments, found %d",n_tokens-1); transform_data_t tmp = t; t = tstack[tstack_ptr--]; t.has_users = t.has_users || tmp.has_users; t.index = state->n_transforms; } else if(strcmp(tokens[0],"LoadIdentity") == 0) { qr_assert(n_tokens==1, "parser", "LoadIdentity takes no arguments, found %d",n_tokens-1); if(t.has_users == true) { t.index = state->n_transforms; t.has_users = false; } } else if(strcmp(tokens[0],"Translate") == 0) { qr_assert(n_tokens==4, "parser", "Translate takes only 3 arguments, found %d",n_tokens-1); if(t.has_users == true) { t.index = state->n_transforms; t.has_users = false; } transform_t tmp; translate(make_vec3(to_float(tokens[1]),to_float(tokens[2]),to_float(tokens[3])), &tmp); mul_matrix(&tmp.m, &t.t.m, &t.t.m); mul_matrix(&tmp.m_inv, &t.t.m_inv, &t.t.m_inv); } else if(strcmp(tokens[0],"Scale") == 0) { qr_assert(n_tokens==4, "parser", "Scale takes only 3 arguments, found %d",n_tokens-1); if(t.has_users == true) { t.index = state->n_transforms; t.has_users = false; } transform_t tmp; scale(make_vec3(to_float(tokens[1]),to_float(tokens[2]),to_float(tokens[3])), &tmp); mul_matrix(&tmp.m, &t.t.m, &t.t.m); } else if(strcmp(tokens[0],"LookAt") == 0) { qr_assert(n_tokens==7, "parser", "LookAt takes only 6 arguments, found %d",n_tokens-1); if(t.has_users == true) { t.index = state->n_transforms; t.has_users = false; } lookat(make_vec3(to_float(tokens[1]),to_float(tokens[2]),to_float(tokens[3])), make_vec3(to_float(tokens[4]),to_float(tokens[5]),to_float(tokens[6])), &t.t); } else if(strcmp(tokens[0],"BSDFDiffuse") == 0) { qr_assert(n_tokens==4, "parser", "BSDFDiffuse takes only 3 arguments, found %d",n_tokens-1); state->n_bsdfs++; realloc_bsdfs(); state->bsdfs[state->n_bsdfs-1] = make_bsdf_diffuse(make_vec3(to_float(tokens[1]), to_float(tokens[2]), to_float(tokens[3]))); bsdf_index = state->n_bsdfs-1; } else if(strcmp(tokens[0],"Sphere") == 0) { qr_assert(n_tokens==2, "parser", "Sphere takes only one argument, found %d",n_tokens-1); state->primitives = (primitive_t*)realloc(state->primitives, sizeof(primitive_t)*(++state->n_primitives)); primitive_t p; p.type = PRIMITIVE_SPHERE; p.data = make_primitive_sphere(to_float(tokens[1])); p.t = t.index; p.bsdf = bsdf_index; t.has_users = true; state->primitives[state->n_primitives-1] = p; state->transforms[t.index] = t.t; state->n_transforms++; realloc_transforms(); } else if(strcmp(tokens[0],"Mesh") == 0) { qr_assert(n_tokens==2, "parser", "Mesh takes only one argument, found %d",n_tokens-1); state->transforms[t.index] = t.t; t.has_users = true; state->n_transforms++; realloc_transforms(); load_mesh(tokens[1], state, t, bsdf_index, NULL); } else if(strcmp(tokens[0],"MeshMat") == 0) { qr_assert(n_tokens==3, "parser", "MeshMat takes only 2 arguments, found %d",n_tokens-1); t.has_users = true; state->transforms[t.index] = t.t; state->n_transforms++; realloc_transforms(); load_mesh(tokens[1], state, t, bsdf_index, tokens[2]); } else if(strcmp(tokens[0],"PointLight") == 0) { qr_assert(n_tokens==7, "parser", "PointLight takes only 6 arguments, found %d",n_tokens-1); realloc_lights(); state->lights[state->n_lights++] = make_light_point(make_vec3(to_float(tokens[1]),to_float(tokens[2]),to_float(tokens[3])), make_vec3(to_float(tokens[4]),to_float(tokens[5]),to_float(tokens[6]))); } else if(strcmp(tokens[0],"SphereLight") == 0) { qr_assert(n_tokens==8, "parser", "SphereLight takes only 7 arguments, found %d",n_tokens-1); realloc_lights(); state->lights[state->n_lights++] = make_light_sphere(make_vec3(to_float(tokens[1]),to_float(tokens[2]),to_float(tokens[3])), to_float(tokens[4]), make_vec3(to_float(tokens[5]),to_float(tokens[6]),to_float(tokens[7]))); } else if(strcmp(tokens[0],"Camera") == 0) { qr_assert(n_tokens==2, "parser", "Camera takes only one argument, found %d",n_tokens-1); state->camera_fplane = to_float(tokens[1]); state->camera_transform = t.index; t.has_users = true; state->camera_origin = transform_point(make_vec3(0.f,0.f,0.f), t.t, true); state->transforms[t.index] = t.t; state->n_transforms++; realloc_transforms(); } else { ERROR("parser", "Unknown directive: %s",tokens[0]); } } } } ERROR("parser", "File ended in the middle of a World section"); }
void read_values(modbus_param_t * mb_param) { int ret; float EV_reg, EV_float, EV_floatlb_trip, EV_float_cancel, EV_eq; unsigned short Et_float, Et_floatlb, Et_float_exit_cum, Et_eqcalendar, Et_eq_above, Et_eq_reg; float EV_reg2, EV_float2, EV_floatlb_trip2, EV_float_cancel2, EV_eq2; unsigned short Et_float2, Et_floatlb2, Et_float_exit_cum2, Et_eqcalendar2, Et_eq_above2, Et_eq_reg2; float EV_tempcomp, EV_hvd, EV_hvr, Evb_ref_lim; short ETb_max, ETb_min; float EV_lvd, EV_lvr, EV_lhvd, EV_lhvr, ER_icomp, Et_lvd_warn; float EV_soc_y2g, EV_soc_g2y, EV_soc_y2r0, EV_soc_r2y; unsigned short Emodbus_id, Emeter_id; float Eic_lim; unsigned int Ehourmeter; short Etmr_eqcalendar; float EAhl_r, EAhl_t, EAhc_r, EAhc_t, EkWhc, EVb_min, EVb_max, EVa_max; uint16_t data[50]; // TODO: use the dispatch table regs, instead of this cut/paste stuff. /* Read the 0xE000 EEPROM Registers and convert the results to their proper values */ ret = read_input_registers(mb_param, SUNSAVERMPPT, 0xE000, 11, data); printf("EEPROM Registers\n\n"); printf("Charge Settings (bank 1)\n"); EV_reg = to_float(data[0]); printf("EV_reg = %.2f V\n",EV_reg); EV_float = to_float(data[1]); printf("EV_float = %.2f V\n",EV_float); Et_float = data[2]; printf("Et_float = %d s\n",Et_float); Et_floatlb = data[3]; printf("Et_floatlb = %d s\n",Et_floatlb); EV_floatlb_trip = to_float(data[4]); printf("EV_floatlb_trip = %.2f V\n",EV_floatlb_trip); EV_float_cancel = to_float(data[5]); printf("EV_float_cancel = %.2f V\n",EV_float_cancel); Et_float_exit_cum = data[6]; printf("Et_float_exit_cum = %d s\n",Et_float_exit_cum); EV_eq = to_float(data[7]); printf("EV_eq = %.2f V\n",EV_eq); Et_eqcalendar = data[8]; printf("Et_eqcalendar = %d days\n",Et_eqcalendar); Et_eq_above = data[9]; printf("Et_eq_above = %d s\n",Et_eq_above); Et_eq_reg = data[10]; printf("Et_eq_reg = %d s\n",Et_eq_reg); /* Read the 0xE00D EEPROM Registers and convert the results to their proper values */ ret = read_input_registers(mb_param, SUNSAVERMPPT, 0xE00D, 11, data); printf("\nCharge Settings (bank 2)\n"); EV_reg2 = to_float(data[0]); printf("EV_reg2 = %.2f V\n",EV_reg2); EV_float2 = to_float(data[1]); printf("EV_float2 = %.2f V\n",EV_float2); Et_float2 = data[2]; printf("Et_float2 = %d s\n",Et_float2); Et_floatlb2 = data[3]; printf("Et_floatlb2 = %d s\n",Et_floatlb2); EV_floatlb_trip2 = to_float(data[4]); printf("EV_floatlb_trip2 = %.2f V\n",EV_floatlb_trip2); EV_float_cancel2 = to_float(data[5]); printf("EV_float_cancel2 = %.2f V\n",EV_float_cancel2); Et_float_exit_cum2 = data[6]; printf("Et_float_exit_cum2 = %d s\n",Et_float_exit_cum2); EV_eq2 = to_float(data[7]); printf("EV_eq2 = %.2f V\n",EV_eq2); Et_eqcalendar2 = data[8]; printf("Et_eqcalendar2 = %d days\n",Et_eqcalendar2); Et_eq_above2 = data[9]; printf("Et_eq_above2 = %d s\n",Et_eq_above2); Et_eq_reg2 = data[10]; printf("Et_eq_reg2 = %d s\n",Et_eq_reg2); /* Read the 0xE01A EEPROM Registers and convert the results to their proper values */ ret = read_input_registers(mb_param, SUNSAVERMPPT, 0xE01A, 6, data); printf("\nCharge Settings (shared)\n"); EV_tempcomp = unsigned_to_float(data[0]); printf("EV_tempcomp = %.2f V\n",EV_tempcomp); EV_hvd = to_float(data[1]); printf("EV_hvd = %.2f V\n",EV_hvd); EV_hvr = to_float(data[2]); printf("EV_hvr = %.2f V\n",EV_hvr); Evb_ref_lim = to_float(data[3]); printf("Evb_ref_lim = %.2f V\n",Evb_ref_lim); ETb_max = data[4]; printf("ETb_max = %d °C\n",ETb_max); ETb_min = data[5]; printf("ETb_min = %d °C\n",ETb_min); /* Read the 0xE022 EEPROM Registers and convert the results to their proper values */ ret = read_input_registers(mb_param, SUNSAVERMPPT, 0xE022, 6, data); printf("\nLoad Settings\n"); EV_lvd = to_float(data[0]); printf("EV_lvd = %.2f V\n",EV_lvd); EV_lvr = to_float(data[1]); printf("EV_lvr = %.2f V\n",EV_lvr); EV_lhvd = to_float(data[2]); printf("EV_lhvd = %.2f V\n",EV_lhvd); EV_lhvr = to_float(data[3]); printf("EV_lhvr = %.2f V\n",EV_lhvr); ER_icomp = data[4]*1.263/65536.0; printf("ER_icomp = %.2f ohms\n",ER_icomp); Et_lvd_warn = data[5]*0.1; printf("Et_lvd_warn = %.2f s\n",Et_lvd_warn); /* Read the 0xE030 EEPROM Registers and convert the results to their proper values */ ret = read_input_registers(mb_param, SUNSAVERMPPT, 0xE030, 6, data); printf("\nMisc Settings\n"); EV_soc_y2g = to_float(data[0]); printf("EV_soc_y2g = %.2f V\n",EV_soc_y2g); EV_soc_g2y = to_float(data[1]); printf("EV_soc_g2y = %.2f V\n",EV_soc_g2y); EV_soc_y2r0 = to_float(data[2]); printf("EV_soc_y2r0 = %.2f V\n",EV_soc_y2r0); EV_soc_r2y = to_float(data[3]); printf("EV_soc_r2y = %.2f V\n",EV_soc_r2y); Emodbus_id = data[4]; printf("Emodbus_id = %d\n",Emodbus_id); Emeter_id = data[5]; printf("Emeter_id = %d\n",Emeter_id); /* Read the 0xE038 EEPROM Registers and convert the results to their proper values */ ret = read_input_registers(mb_param, SUNSAVERMPPT, 0xE038, 1, data); printf("\nPPT Settings\n"); Eic_lim = fl_conv_2(data[0]); printf("Eic_lim = %.2f A\n",Eic_lim); /* Read the 0xE040 EEPROM Registers and convert the results to their proper values */ ret = read_input_registers(mb_param, SUNSAVERMPPT, 0xE040, 15, data); printf("\nRead only section of EEPROM\n"); Ehourmeter = shift_32(data[1], data[0]); printf("Ehourmeter = %d h\n",Ehourmeter); EAhl_r = shift_float(data[3], data[2]); printf("EAhl_r = %.2f Ah\n",EAhl_r); EAhl_t = shift_float(data[5], data[4]); printf("EAhl_t = %.2f Ah\n",EAhl_t); EAhc_r = shift_float(data[7], data[6]); printf("EAhc_r = %.2f Ah\n",EAhc_r); EAhc_t = shift_float(data[9], data[8]); printf("EAhc_t = %.2f Ah\n",EAhc_t); EkWhc = data[10]*0.1; printf("EkWhc = %.2f kWh\n",EkWhc); EVb_min = to_float(data[11]); printf("EVb_min = %.2f V\n",EVb_min); EVb_max = to_float(data[12]); printf("EVb_max = %.2f V\n",EVb_max); EVa_max = to_float(data[13]); printf("EVa_max = %.2f V\n",EVa_max); Etmr_eqcalendar = data[14]; printf("Etmr_eqcalendar = %d days\n",Etmr_eqcalendar); }
float as_float() const { return to_float(_args[_index]); }