void genratesignstate()
{
	int V_contri, H_contri, i;

	for( i = 0; i < 256; i++){
		H_contri = contribution( i & H0, i & SIGNH0, i & H1, i & SIGNH1);
		V_contri = contribution( i & V0, i & SIGNV0, i & V1, i & SIGNV1);

		sign_states[i]	= sign_state(H_contri, V_contri);
		XORbit[i]		= xorbit_state(H_contri, V_contri);
	}
}
Example #2
0
void Viewport::applyView( const Viewport& segmentVP, const Viewport& viewVP,
                          const PixelViewport& pvp, const Vector4i& overdraw )
{
    // part of view covered by segment/view channel
    Viewport contribution( segmentVP );
    contribution.intersect( viewVP );
    contribution.transform( viewVP );
        
    // extend by overdraw percentage
    EQASSERT( pvp.hasArea( ));

    const float xDelta(( static_cast< float >( overdraw.x() + pvp.w ) /
                         static_cast< float >( pvp.w ) - 1.0f ) * 
                       contribution.w );
    contribution.x -= xDelta;
    contribution.w += (( static_cast< float >( overdraw.z() + pvp.w ) /
                         static_cast< float >( pvp.w ) - 1.0f ) * 
                       contribution.w ); 
    contribution.w += xDelta;

    const float yDelta(( static_cast< float >( overdraw.y() + pvp.h ) /
                         static_cast< float >( pvp.h ) - 1.0f ) *
                       contribution.h ); 
    contribution.y -= yDelta;
    contribution.h += (( static_cast< float >( overdraw.w() + pvp.h ) /
                         static_cast< float >( pvp.h ) - 1.0f ) *
                       contribution.h ); 
    contribution.h += yDelta;
    
    x = contribution.x + x * contribution.w;
    y = contribution.y + y * contribution.h;
    w *= contribution.w;
    h *= contribution.h;
}
Example #3
0
void
StructuralEngngModel :: computeExternalLoadReactionContribution(FloatArray &reactions, TimeStep *tStep, int di)
{
    int numRestrDofs = this->giveNumberOfPrescribedDomainEquations(di, EID_MomentumBalance);
    reactions.resize(numRestrDofs);
    reactions.zero();
    FloatArray contribution(numRestrDofs);

    reactions.resize( this->giveNumberOfPrescribedDomainEquations(di, EID_MomentumBalance) );
    reactions.zero();
    this->assembleVector( reactions, tStep, EID_MomentumBalance, ExternalForcesVector, VM_Total,
                          EModelDefaultPrescribedEquationNumbering(), this->giveDomain(di) );
}
Example #4
0
void ifaGroup::buildRowSkip()
{
	rowSkip.assign(rowMap.size(), false);

	if (itemDims == 0) return;

	// Rows with no information about an ability will obtain the
	// prior distribution as an ability estimate. This will
	// throw off multigroup latent distribution estimates.
	for (size_t rx=0; rx < rowMap.size(); rx++) {
		bool hasNA = false;
		std::vector<int> contribution(itemDims);
		for (int ix=0; ix < numItems(); ix++) {
			int pick = dataColumn(ix)[ rowMap[rx] ];
			if (pick == NA_INTEGER) {
				hasNA = true;
				continue;
			}
			const double *ispec = spec[ix];
			int dims = ispec[RPF_ISpecDims];
			double *iparam = getItemParam(ix);
			for (int dx=0; dx < dims; dx++) {
				// assume factor loadings are the first item parameters
				if (iparam[dx] == 0) continue;
				contribution[dx] += 1;
			}
		}
		if (!hasNA) continue;
		if (minItemsPerScore == NA_INTEGER) {
			mxThrow("You have missing data. You must set minItemsPerScore");
		}
		for (int ax=0; ax < itemDims; ++ax) {
			if (contribution[ax] < minItemsPerScore) {
				// We could compute the other scores, but estimation of the
				// latent distribution is in the hot code path. We can reconsider
				// this choice when we try generating scores instead of the
				// score distribution.
				rowSkip[rx] = true;
			}
		}
	}
}
Example #5
0
 void PathTracingRenderer::Job::kernel(uint32_t threadID) {
     ArenaAllocator &mem = mems[threadID];
     IndependentLightPathSampler &pathSampler = *pathSamplers[threadID];
     for (int ly = 0; ly < numPixelY; ++ly) {
         for (int lx = 0; lx < numPixelX; ++lx) {
             float time = pathSampler.getTimeSample(timeStart, timeEnd);
             PixelPosition p = pathSampler.getPixelPositionSample(basePixelX + lx, basePixelY + ly);
             
             float selectWLPDF;
             WavelengthSamples wls = WavelengthSamples::createWithEqualOffsets(pathSampler.getWavelengthSample(), pathSampler.getWLSelectionSample(), &selectWLPDF);
             
             LensPosQuery lensQuery(time, wls);
             LensPosQueryResult lensResult;
             SampledSpectrum We0 = camera->sample(lensQuery, pathSampler.getLensPosSample(), &lensResult);
             
             IDFSample WeSample(p.x / imageWidth, p.y / imageHeight);
             IDFQueryResult WeResult;
             IDF* idf = camera->createIDF(lensResult.surfPt, wls, mem);
             SampledSpectrum We1 = idf->sample(WeSample, &WeResult);
             
             Ray ray(lensResult.surfPt.p, lensResult.surfPt.shadingFrame.fromLocal(WeResult.dirLocal), time);
             SampledSpectrum C = contribution(*scene, wls, ray, pathSampler, mem);
             SLRAssert(C.hasNaN() == false && C.hasInf() == false && C.hasMinus() == false,
                       "Unexpected value detected: %s\n"
                       "pix: (%f, %f)", C.toString().c_str(), px, py);
             
             SampledSpectrum weight = (We0 * We1) * (absDot(ray.dir, lensResult.surfPt.gNormal) / (lensResult.areaPDF * WeResult.dirPDF * selectWLPDF));
             SLRAssert(weight.hasNaN() == false && weight.hasInf() == false && weight.hasMinus() == false,
                       "Unexpected value detected: %s\n"
                       "pix: (%f, %f)", weight.toString().c_str(), px, py);
             sensor->add(p.x, p.y, wls, weight * C);
             
             mem.reset();
         }
     }
 }
Example #6
0
void KmerHash::buildKmerTable(string genomePath) {
    ifstream genomeFile(genomePath.c_str(), ios::in);
    string geneSeq = "";
    string line;
    while(getline(genomeFile, line)) {
        if(line[0] != '>') {
            geneSeq += boost::trim_copy(line);
        }
    }
    genomeFile.close();

    //int gp = 0;
    //long long gid = 0;
    //for(; gp < K - 1; gp ++) {
    //    gid = (gid << 2) + parseBP(geneSeq[gp]);
    //}
    //for(; gp < geneSeq.size(); gp ++) {
    //    gid = ((gid << 2) + parseBP(geneSeq[gp])) & mask;
    //    kmers.push_back(gid);
    //}

    for(int g = 0; g < NG; g++) {
        //cout << "Gene-" << g << " processed..." << endl;

        for(int e = 0; e < NE[g]; e++) {
            ostringstream oss;
            oss << g << "," << e << "," << e;
            string exonid = oss.str();
            int exonst = geneBoundary[g][e].first;
            int exoned = geneBoundary[g][e].second + 1;
            
            int p = 0;
            long long id = 0;
            double tot = (exoned - exonst - readLength + 1) * (readLength - K + 1);
            
            //cout << "Exon start site: " << exonst << endl;
            //for(int i = exonst ; i < exoned; i++) {
            //    cout << geneSeq[i];
            //} cout << endl;

            for(; p < K - 1; p ++) {
                id = (id << 2) + parseBP(geneSeq[exonst + p]);
            }

            for(; exonst + p < exoned; p ++) {
                id = ((id << 2) + parseBP(geneSeq[exonst + p])) & mask;
                double contr = contribution(0, exoned - exonst, p - K + 1, p + 1, exoned - exonst) / tot;
                if(kmerTable.count(id) > 0) {
                    if(kmerTable[id].count(exonid) > 0) {
                        kmerTable[id][exonid] = kmerTable[id][exonid] + contr;
                    } else {
                        kmerTable[id][exonid] = contr;
                    }
                }
            }
        }
        for(int ei = 0; ei < NE[g]; ei++) {
            for(int ej = ei + 1; ej < NE[g]; ej++) {
                ostringstream oss;
                oss << g << "," << ei << "," << ej;
                string exonid = oss.str();
                int exonedi = geneBoundary[g][ei].second + 1;
                int exonstj = geneBoundary[g][ej].first;

                int p = 0;
                long long id = 0;
                double tot = (readLength - 1) * (readLength - K + 1);
                for(; p < K - 1; p ++) {
                    id = (id << 2) + parseBP(geneSeq[exonedi - readLength + 1 + p]);
                }
                for(; p < readLength - 1; p ++) {
                    id = ((id << 2) + parseBP(geneSeq[exonedi - readLength + 1 + p])) & mask;
                    double contr = contribution(0, 2*readLength - 2, p - K + 1, p + 1, 2*readLength - 2) / tot;
                    if(kmerTable.count(id) > 0) {
                        if(kmerTable[id].count(exonid) > 0) {
                            kmerTable[id][exonid] = kmerTable[id][exonid] + contr;
                        } else {
                            kmerTable[id][exonid] = contr;
                        }
                    }
                }
                for(p = 0; p < readLength - 1; p ++) {
                    id = ((id << 2) + parseBP(geneSeq[exonstj + p])) & mask;
                    double contr = contribution(0, 2*readLength - 2, readLength + p - K, readLength + p, 2*readLength - 2) / tot;
                    if(kmerTable.count(id) > 0) {
                        if(kmerTable[id].count(exonid) > 0) {
                            kmerTable[id][exonid] = kmerTable[id][exonid] + contr;
                        } else {
                            kmerTable[id][exonid] = contr;
                        }
                    }
                }
            }
        }
    }

    cout << "End of building kmer table" << endl;
}
Example #7
0
 void DebugRenderer::Job::kernel(uint32_t threadID) {
     ArenaAllocator &mem = mems[threadID];
     IndependentLightPathSampler &pathSampler = *pathSamplers[threadID];
     for (int ly = 0; ly < numPixelY; ++ly) {
         for (int lx = 0; lx < numPixelX; ++lx) {
             float time = pathSampler.getTimeSample(timeStart, timeEnd);
             PixelPosition p = pathSampler.getPixelPositionSample(basePixelX + lx, basePixelY + ly);
             
             float selectWLPDF;
             WavelengthSamples wls = WavelengthSamples::createWithEqualOffsets(pathSampler.getWavelengthSample(), pathSampler.getWLSelectionSample(), &selectWLPDF);
             
             LensPosQuery lensQuery(time, wls);
             LensPosQueryResult lensResult;
             SampledSpectrum We0 = camera->sample(lensQuery, pathSampler.getLensPosSample(), &lensResult);
             (void)We0;
             
             IDFSample WeSample(p.x / imageWidth, p.y / imageHeight);
             IDFQueryResult WeResult;
             IDF* idf = camera->createIDF(lensResult.surfPt, wls, mem);
             SampledSpectrum We1 = idf->sample(WeSample, &WeResult);
             (void)We1;
             
             Ray ray(lensResult.surfPt.p, lensResult.surfPt.shadingFrame.fromLocal(WeResult.dirLocal), time);
             DebugInfo info = contribution(*scene, wls, ray, pathSampler, mem);
             
             for (int i = 0; i < renderer->m_channels.size(); ++i) {
                 if (!renderer->m_channels[i])
                     continue;
                 auto &chImg = (*chImages)[i];
                 switch ((ExtraChannel)i) {
                     case ExtraChannel::GeometricNormal: {
                         RGB8x3 val;
                         val.r = (uint8_t)std::clamp((0.5f * info.surfPt.gNormal.x + 0.5f) * 255, 0.0f, 255.0f);
                         val.g = (uint8_t)std::clamp((0.5f * info.surfPt.gNormal.y + 0.5f) * 255, 0.0f, 255.0f);
                         val.b = (uint8_t)std::clamp((0.5f * info.surfPt.gNormal.z + 0.5f) * 255, 0.0f, 255.0f);
                         chImg->set((int)p.x, (int)p.y, val);
                         break;
                     }
                     case ExtraChannel::ShadingNormal: {
                         RGB8x3 val;
                         val.r = (uint8_t)std::clamp((0.5f * info.surfPt.shadingFrame.z.x + 0.5f) * 255, 0.0f, 255.0f);
                         val.g = (uint8_t)std::clamp((0.5f * info.surfPt.shadingFrame.z.y + 0.5f) * 255, 0.0f, 255.0f);
                         val.b = (uint8_t)std::clamp((0.5f * info.surfPt.shadingFrame.z.z + 0.5f) * 255, 0.0f, 255.0f);
                         chImg->set((int)p.x, (int)p.y, val);
                         break;
                     }
                     case ExtraChannel::ShadingTangent: {
                         RGB8x3 val;
                         val.r = (uint8_t)std::clamp((0.5f * info.surfPt.shadingFrame.x.x + 0.5f) * 255, 0.0f, 255.0f);
                         val.g = (uint8_t)std::clamp((0.5f * info.surfPt.shadingFrame.x.y + 0.5f) * 255, 0.0f, 255.0f);
                         val.b = (uint8_t)std::clamp((0.5f * info.surfPt.shadingFrame.x.z + 0.5f) * 255, 0.0f, 255.0f);
                         chImg->set((int)p.x, (int)p.y, val);
                         break;
                     }
                     case ExtraChannel::Distance: {
                         Gray8 val;
                         SLRAssert_NotImplemented();
                         chImg->set((int)p.x, (int)p.y, val);
                         break;
                     }
                     default:
                         break;
                 }
             }
             
             mem.reset();
         }
     }
 }
Example #8
0
void analyzeVideo(const std::string& folder, const Camera& calibrated_camera, float label_width)
{
    // Start with a single hypotheses of the cube.
    std::vector<ProbabalisticCube> cube_hypotheses;
    cube_hypotheses.push_back(ProbabalisticCube());
    for (int frame_i = 0;;)
    {
        printf("Frame %i\n", frame_i);

        char buf[1024];
        sprintf(buf, "%s/frame%05d.png", folder.c_str(), frame_i);
        cv::Mat3b img = cv::imread(buf, cv::IMREAD_COLOR);
        if (img.empty())
        {
            break;
        }

        const size_t num_hypotheses_before = cube_hypotheses.size();
        printf("Num hypotheses before: %lu\n", num_hypotheses_before);

        cube_hypotheses = predict(cube_hypotheses);
        const size_t num_hypotheses_after = cube_hypotheses.size();
        printf("Num hypotheses after:  %lu\n", num_hypotheses_after);
        printf("Brancing factor:  %f\n", double(num_hypotheses_after) / num_hypotheses_before);

        const size_t max_hypotheses = 216;
        if (num_hypotheses_after > max_hypotheses)
        {
            const size_t pruned_num = num_hypotheses_after - max_hypotheses;
            const double removed_percentage = pruned_num * 100.0 / num_hypotheses_after;
            printf("Pruning to %lu hypotheses, removing %lu (%.1f%%) hypotheses.\n",
                max_hypotheses, pruned_num, removed_percentage);
        }
        prune(cube_hypotheses, max_hypotheses);

        printf("Most likely cubes:\n");
        for (int i = 0; i < cube_hypotheses.size() && i < 5; ++i)
        {
            const ProbabalisticCube& cube = cube_hypotheses[i];
            const std::string permutation = cube.cube_permutation.to_String();
            const double likelihood_percent = exp(cube.log_likelihood) * 100.0;
            printf("%d: %s %3.5f%%\n", i, permutation.c_str(), likelihood_percent);
        }

        std::vector<LabelContour> labels = findLabelContours(img, 12, true);
        std::vector<std::vector<cv::Point2f>> detected_corners = findLabelCorners(labels);

        std::vector<Camera> all_camera_candidates;
        for (const auto& corners : detected_corners)
        {
            std::vector<Camera> cameras = predictCameraPosesForLabel(calibrated_camera, corners, label_width);
            all_camera_candidates.insert(all_camera_candidates.end(), cameras.begin(), cameras.end());
        }

        std::vector<double> camera_scores;
        camera_scores.reserve(all_camera_candidates.size());
        {
            cv::Mat1f accumulation(img.size(), 0.f);
            for (const auto& cam : all_camera_candidates)
            {
                cv::Mat1f contribution(img.size(), 0.f);
                std::vector<cv::Point2f> predicted_corners = projectCubeCorners(cam, label_width);
                double score = scorePredictedCorners(predicted_corners, detected_corners);
                camera_scores.push_back(score);

                for (int i = 0; i < predicted_corners.size(); i += 4)
                {
                    std::vector<cv::Point> corners = {
                        predicted_corners[i + 0],
                        predicted_corners[i + 1],
                        predicted_corners[i + 2],
                        predicted_corners[i + 3],
                    };
                    cv::polylines(contribution, corners, true, cv::Scalar(score * score));
                }
                accumulation += contribution;
            }
            double minval;
            double maxval;
            cv::minMaxLoc(accumulation, &minval, &maxval);
            cv::imshow("accumulation", accumulation / maxval);
        }
        if (!camera_scores.empty())
        {
            std::vector<double> sorted_camera_scores = camera_scores;
            std::sort(sorted_camera_scores.begin(), sorted_camera_scores.end());
            std::reverse(sorted_camera_scores.begin(), sorted_camera_scores.end());
            printf("Min score: %f max score: %f\n", sorted_camera_scores.back(), sorted_camera_scores.front());

            for (int i = 0; i < sorted_camera_scores.size() && i < 5; ++i)
            {
                double score = sorted_camera_scores[i];
                printf("#%d score: %f\n", i + 1, score);
            }
        }

        if (!all_camera_candidates.empty())
        {
            const size_t index = std::distance(camera_scores.begin(),
                std::max_element(camera_scores.begin(), camera_scores.end()));
            printf("detected_corners size: %lu index: %lu\n", detected_corners.size(), index);

            const Camera& cam = all_camera_candidates[index];
            std::vector<cv::Point2f> predicted_corners = projectCubeCorners(cam, label_width);

            cv::Mat3b canvas = img * 0.25f;
            for (int i = 0; i < predicted_corners.size(); i += 4)
            {
                std::vector<cv::Point> corners = {
                    predicted_corners[i + 0],
                    predicted_corners[i + 1],
                    predicted_corners[i + 2],
                    predicted_corners[i + 3],
                };
                cv::polylines(canvas, corners, true, cv::Scalar(0, 0, 255));
            }

            {
                std::vector<cv::Point> corners = {
                    detected_corners[index / 9][0],
                    detected_corners[index / 9][1],
                    detected_corners[index / 9][2],
                    detected_corners[index / 9][3],
                };
                cv::polylines(canvas, corners, true, cv::Scalar(255, 0, 255));
            }

            cv::imshow("predicted labels", canvas);
        }

        {
            cv::Mat3b canvas = img * 0.25f;
            drawLabels(canvas, labels, cv::Scalar(255, 255, 255));

            for (const auto& corners : detected_corners)
            {
                cv::polylines(canvas, cast<cv::Point>(corners), true, cv::Scalar(0, 0, 255));
                for (size_t i = 0; i < corners.size(); ++i)
                {
                    char text[12];
                    sprintf(text, "%lu", i);
                    cv::putText(canvas, text, corners[i],
                        cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 0, 255));
                }
            }

            cv::imshow("detected labels", canvas);
        }

        if (int key = cv::waitKey(0) & 255)
        {
            if (key == 27)
            {
                break; // stop by pressing ESC
            }
            if (key == 32) // space
            {
                ++frame_i;
            }
            if (key == 83) // right arrow
            {
                ++frame_i;
            }
            if (key == 82) // up arrow
            {

            }
            if (key == 81) // left arrow
            {
                --frame_i;
            }
            if (key == 84) // down arrow
            {

            }
            if (key != 255)
            {
                printf("Key: %d\n", key);
                fflush(stdout);
            }
        }
    }
}