/** * Copy a piece of map and instantly paste at given location. * * @param tile Tile where to paste (northern). * @param flags Command flags. * @param p1 Various bits: * \li bits 0..27 [28] - northern tile of the source area * \li bits 28..31 [4] - rail type (RailType) to convert to, ignored if CPM_CONVERT_RAILTYPE mode is off * @param p2 Various bits: * \li bits 0..5 [6] - source area width * \li bits 6..11 [6] - source area height * \li bits 12..15 [4] - additional amount of tile heights to add to each tile (-8..7) * \li bits 16..18 [3] - transformation to perform (DirTransformation) * \li bits 19..27 [9] - mode (CopyPasteMode) * \li bits 28..31 [4] - [ unused ] * @param text Unused. * @return The cost of this operation or an error. */ CommandCost CmdInstantCopyPaste(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { CopyPasteParams copy_paste; /* extract and validate source area */ copy_paste.src_area.tile = GenericTileIndex(TileIndex(GB(p1, 0, 28))); copy_paste.src_area.w = GB(p2, 0, 6); copy_paste.src_area.h = GB(p2, 6, 6); CommandCost ret = ValParamCopyPasteArea(copy_paste.src_area); if (ret.Failed()) return ret; /* calculate and validate destination area */ copy_paste.dst_area = TransformTileArea(copy_paste.src_area, GenericTileIndex(tile), copy_paste.transformation); ret = ValParamCopyPasteArea(copy_paste.dst_area); if (ret.Failed()) return ret; /* extract and validate copy/paste mode */ copy_paste.mode = (CopyPasteMode)GB(p2, 19, 9); if (!ValParamCopyPasteMode(copy_paste.mode)) return CMD_ERROR; /* extract and validate rail type */ copy_paste.railtype = (RailType)GB(p1, 28, 4); if (!ValParamRailtype(copy_paste.railtype)) return CMD_ERROR; /* extract transformation */ copy_paste.transformation = (DirTransformation)GB(p2, 16, 3); /* extract the additional number of height units */ int additional_height_delta = GB(p2, 12, 4); // this is a 4-bit SIGNED integer (-8..7) additional_height_delta |= -(additional_height_delta & (1 << 3)); // propagate the sign bit /* calculate the height */ copy_paste.height_delta = CalcCopyPasteHeightDelta(copy_paste.src_area, copy_paste.dst_area, copy_paste.transformation, additional_height_delta); /* when copy and paste areas are too close each other, firstly * copy to the clipboard and then from the clipboard to the map */ if (CopyPasteAreasMayColide(copy_paste)) { Map *clipboard = GetClipboardBuffer(INSTANT_COPY_PASTE_BUFFER); /* Copy to a buffer, but only in the first stage of the command. * In a single player game and also while we are a server, the first one is non-DC_EXEC * stage (which is fallowed then by a DC_EXEC stage). When we are a client, there is only * one stage which is either a single non-DC_EXEC stage (shift pressed), or a single DC_EXEC * stage (command comming from the network). */ if ((_networking && !_network_server) || !(flags & DC_EXEC)) { CopyToClipboard(clipboard, copy_paste.src_area); } /* paste from the clipboard */ ret = PasteFromClipboard(clipboard, tile, flags, copy_paste.mode, copy_paste.transformation, copy_paste.railtype, additional_height_delta); } else { /* copy/paste directly */ InitializePasting(flags, copy_paste); DoCopyPaste(copy_paste); ret = FinalizePasting(); } return ret; }
TileIndex TileIndex::fromCoordinates(const GeoIface::GeoCoordinates& coordinate, const int getLevel) { GEOIFACE_ASSERT(getLevel<=MaxLevel); if (!coordinate.hasCoordinates()) return TileIndex(); qreal tileLatBL = -90.0; qreal tileLonBL = -180.0; qreal tileLatHeight = 180.0; qreal tileLonWidth = 360.0; TileIndex resultIndex; for (int l = 0; l <= getLevel; ++l) { // how many tiles at this level? const qreal latDivisor = TileIndex::Tiling; const qreal lonDivisor = TileIndex::Tiling; const qreal dLat = tileLatHeight / latDivisor; const qreal dLon = tileLonWidth / lonDivisor; int latIndex = int( (coordinate.lat() - tileLatBL ) / dLat ); int lonIndex = int( (coordinate.lon() - tileLonBL ) / dLon ); // protect against invalid indices due to rounding errors bool haveRoundingErrors = false; if (latIndex < 0) { haveRoundingErrors = true; latIndex = 0; } if (lonIndex < 0) { haveRoundingErrors = true; lonIndex = 0; } if (latIndex >= latDivisor) { haveRoundingErrors = true; latIndex = latDivisor-1; } if (lonIndex >= lonDivisor) { haveRoundingErrors = true; lonIndex = lonDivisor-1; } if (haveRoundingErrors) { // qCDebug(DIGIKAM_GEOIFACE_LOG) << QString::fromLatin1("Rounding errors at level %1!").arg(l); } resultIndex.appendLatLonIndex(latIndex, lonIndex); // update the start position for the next tile: // TODO: rounding errors tileLatBL += latIndex*dLat; tileLonBL += lonIndex*dLon; tileLatHeight /= latDivisor; tileLonWidth /= lonDivisor; } return resultIndex; }
int main(int argc, char **argv) { long long begin_time = millitime(); char **argptr = argv+1; if (!*argptr) usage(); std::string storename = *argptr++; if (!*argptr) usage(); int uid = atoi(*argptr++); set_log_prefix(string_printf("%d %d ", getpid(), uid)); if (!*argptr) usage(); std::string full_channel_name = *argptr++; #if FFT_SUPPORT bool writing_fft = false; size_t fftpos = full_channel_name.rfind(".DFT"); if (fftpos != std::string::npos) { full_channel_name = full_channel_name.substr(0, fftpos); writing_fft = true; } #endif /* FFT_SUPPORT */ if (!*argptr) usage(); int tile_level = atoi(*argptr++); if (!*argptr) usage(); long long tile_offset = atoll(*argptr++); if (*argptr) usage(); // Desired level and offset // Translation between tile request and tilestore: // tile: level 0 is 512 samples in 512 seconds // store: level 0 is 65536 samples in 1 second // for tile level 0, we want to get store level 14, which is 65536 samples in 16384 seconds // Levels differ by 9 between client and server TileIndex client_tile_index = TileIndex(tile_level+9, tile_offset); { std::string arglist; for (int i = 0; i < argc; i++) { if (i) arglist += " "; arglist += std::string("'")+argv[i]+"'"; } log_f("gettile START: %s (time %.9f-%.9f)", arglist.c_str(), client_tile_index.start_time(), client_tile_index.end_time()); } FilesystemKVS store(storename.c_str()); // 5th ancestor TileIndex requested_index = client_tile_index.parent().parent().parent().parent().parent(); std::vector<DataSample<double> > double_samples; std::vector<DataSample<std::string> > string_samples; std::vector<DataSample<std::string> > comments; bool doubles_binned, strings_binned, comments_binned; // TODO: If writing FFT, ***get more data*** // TODO: Use min_time_required and max_time_required, get max-res data read_tile_samples(store, uid, full_channel_name, requested_index, client_tile_index, double_samples, doubles_binned); #if FFT_SUPPORT if (writing_fft) { std::vector<std::vector<double> > fft, shifted; int num_values; windowed_fft(double_samples, requested_index, fft); present_fft(fft, shifted, num_values); // JSON tile to send back to the client includes some of the same // information as a non-DFT tile Json::Value tile(Json::objectValue); tile["level"] = Json::Value(tile_level); // See discussion below for reason to cast tile_offset // from long long to double tile["offset"] = Json::Value((double)tile_offset); tile["num_values"] = Json::Value(num_values); tile["dft"] = Json::Value(Json::arrayValue); for (unsigned window_id = 0; window_id < shifted.size(); window_id++) { Json::Value window(Json::arrayValue); for (unsigned i = 0; i < shifted[window_id].size(); i++) window.append(shifted[window_id][i]); tile["dft"].append(window); } std::cout << Json::FastWriter().write(tile) << std::endl; return 0; } #endif /* FFT_SUPPORT */ read_tile_samples(store, uid, full_channel_name, requested_index, client_tile_index, string_samples, strings_binned); read_tile_samples(store, uid, full_channel_name+"._comment", requested_index, client_tile_index, comments, comments_binned); string_samples.insert(string_samples.end(), comments.begin(), comments.end()); std::sort(string_samples.begin(), string_samples.end(), DataSample<std::string>::time_lessthan); std::map<double, DataSample<double> > double_sample_map; for (unsigned i = 0; i < double_samples.size(); i++) { double_sample_map[double_samples[i].time] = double_samples[i]; // TODO: combine if two samples at same time? } std::set<double> has_string; for (unsigned i = 0; i < string_samples.size(); i++) { has_string.insert(string_samples[i].time); } std::vector<GraphSample> graph_samples; bool has_fifth_col = string_samples.size()>0; for (unsigned i = 0; i < string_samples.size(); i++) { if (double_sample_map.find(string_samples[i].time) != double_sample_map.end()) { GraphSample gs(double_sample_map[string_samples[i].time]); gs.has_comment = true; gs.comment = string_samples[i].value; graph_samples.push_back(gs); } else { graph_samples.push_back(GraphSample(string_samples[i])); } } for (unsigned i = 0; i < double_samples.size(); i++) { if (has_string.find(double_samples[i].time) == has_string.end()) { graph_samples.push_back(GraphSample(double_samples[i])); } } std::sort(graph_samples.begin(), graph_samples.end()); double bin_width = client_tile_index.duration() / 512.0; double line_break_threshold = bin_width * 4.0; if (!doubles_binned && double_samples.size() > 1) { // Find the median distance between samples std::vector<double> spacing(double_samples.size()-1); for (size_t i = 0; i < double_samples.size()-1; i++) { spacing[i] = double_samples[i+1].time - double_samples[i].time; } std::sort(spacing.begin(), spacing.end()); double median_spacing = spacing[spacing.size()/2]; // Set line_break_threshold to larger of 4*median_spacing and 4*bin_width line_break_threshold = std::max(line_break_threshold, median_spacing * 4); } if (graph_samples.size()) { log_f("gettile: outputting %zd samples", graph_samples.size()); Json::Value tile(Json::objectValue); tile["level"] = Json::Value(tile_level); // An aside about offset type and precision: // JSONCPP doesn't have a long long type; to preserve full resolution we need to convert to double here. As Javascript itself // will read this as a double-precision value, we're not introducing a problem. // For a detailed discussion, see https://sites.google.com/a/bodytrack.org/wiki/website/tile-coordinates-and-numeric-precision // Irritatingly, JSONCPP wants to add ".0" to the end of floating-point numbers that don't need it. This is inconsistent // with Javascript itself and simply introduces extra bytes to the representation tile["offset"] = Json::Value((double)tile_offset); tile["fields"] = Json::Value(Json::arrayValue); tile["fields"].append(Json::Value("time")); tile["fields"].append(Json::Value("mean")); tile["fields"].append(Json::Value("stddev")); tile["fields"].append(Json::Value("count")); if (has_fifth_col) tile["fields"].append(Json::Value("comment")); Json::Value data(Json::arrayValue); double previous_sample_time = client_tile_index.start_time(); bool previous_had_value = true; for (unsigned i = 0; i < graph_samples.size(); i++) { // TODO: improve linebreak calculations: // 1) observe channel specs line break size from database (expressed in time; some observations have long time periods and others short) // 2) insert breaks at beginning or end of tile if needed // 3) should client be the one to decide where line breaks are (if we give it the threshold?) if (graph_samples[i].time - previous_sample_time > line_break_threshold || !graph_samples[i].has_value || !previous_had_value) { // Insert line break, which has value -1e+308 Json::Value sample = Json::Value(Json::arrayValue); sample.append(Json::Value(0.5*(graph_samples[i].time+previous_sample_time))); sample.append(Json::Value(-1e308)); sample.append(Json::Value(0)); sample.append(Json::Value(0)); if (has_fifth_col) sample.append(Json::Value()); // NULL data.append(sample); } previous_sample_time = graph_samples[i].time; previous_had_value = graph_samples[i].has_value; { Json::Value sample = Json::Value(Json::arrayValue); sample.append(Json::Value(graph_samples[i].time)); sample.append(Json::Value(graph_samples[i].has_value ? graph_samples[i].value : 0.0)); // TODO: fix datastore so we never see NAN crop up here! sample.append(Json::Value(isnan(graph_samples[i].stddev) ? 0 : graph_samples[i].stddev)); sample.append(Json::Value(graph_samples[i].weight)); if (has_fifth_col) { sample.append(graph_samples[i].has_comment ? Json::Value(graph_samples[i].comment) : Json::Value()); } data.append(sample); } } if (client_tile_index.end_time() - previous_sample_time > line_break_threshold || !previous_had_value) { // Insert line break, which has value -1e+308 Json::Value sample = Json::Value(Json::arrayValue); sample.append(Json::Value(0.5*(previous_sample_time + client_tile_index.end_time()))); sample.append(Json::Value(-1e308)); sample.append(Json::Value(0)); sample.append(Json::Value(0)); if (has_fifth_col) sample.append(Json::Value()); // NULL data.append(sample); } tile["data"] = data; // only include the sample_width field if we actually binned if (doubles_binned) { tile["sample_width"] = bin_width; } printf("%s\n", rtrim(Json::FastWriter().write(tile)).c_str()); } else { log_f("gettile: no samples"); printf("{}"); } log_f("gettile: finished in %lld msec", millitime() - begin_time); return 0; }
TileIndex operator +(const TileOffset& inTileOffset, const TileIndex& inTileIndex) { return TileIndex(TileIndex::pod(inTileOffset) + TileIndex::pod(inTileIndex)); }