Esempio n. 1
0
 types::ndarray<
     typename __combined<typename types::numpy_expr_to_ndarray<E>::T,
                         typename types::numpy_expr_to_ndarray<F>::T>::type,
     1>
 intersect1d(E const &e, F const &f)
 {
   using T = typename __combined<
       typename types::numpy_expr_to_ndarray<E>::T,
       typename types::numpy_expr_to_ndarray<F>::T>::type;
   auto ae = asarray(e);
   auto af = asarray(f);
   std::set<T> sae(ae.fbegin(), ae.fend());
   std::set<T> found;
   types::list<T> lout(0);
   lout.reserve(sae.size());
   for (auto iter = af.fbegin(), end = af.fend(); iter != end; ++iter) {
     auto curr = *iter;
     if (sae.find(curr) != sae.end() and found.find(curr) == found.end()) {
       found.insert(curr);
       lout.push_back(curr);
     }
   }
   std::sort(lout.begin(), lout.end());
   return types::ndarray<T, 1>(lout);
 }
Esempio n. 2
0
int main(int argc, char** argv)
{
#ifdef PARALLEL_CORES
  omp_set_num_threads(PARALLEL_CORES);
#endif

  std::string directory = "./";
  if(argc > 1)
    directory = std::string(argv[1]);

  IDXLoader loader(28, 28, 10000, 1, directory);
  OpenANN::DirectStorageDataSet trainSet(&loader.trainingInput,
                                         &loader.trainingInput);

  int H = 196;
  OpenANN::SparseAutoEncoder sae(loader.D, H, 3.0, 0.1, 3e-3, OpenANN::LOGISTIC);
  sae.trainingSet(trainSet);

  OpenANN::LBFGS optimizer(20);
  OpenANN::StoppingCriteria stop;
  stop.maximalIterations = 400;
  optimizer.setOptimizable(sae);
  optimizer.setStopCriteria(stop);
  optimizer.optimize();

  OpenANN::MulticlassEvaluator evaluator(1, OpenANN::Logger::FILE);
  OpenANN::DirectStorageDataSet testSet(&loader.testInput, &loader.testInput,
                                        &evaluator);
  sae.validationSet(testSet);

  QApplication app(argc, argv);
  SparseAutoEncoderVisualization visual(sae, trainSet, H, 5, 7, 800, 600);
  visual.show();
  visual.resize(800, 600);
  return app.exec();
}
Esempio n. 3
0
int main(int argc, char **argv) {
    AlignmentIncidenceMatrix *aim;
    SampleAllelicExpression::model model = SampleAllelicExpression::MODEL_1;

    clock_t t1, t2;
    float diff;

    int m;
    int max_iterations = 999;
    int num_iterations;
    int all_samples = 1;
    int sample_start = 0;
    int sample_end = 0;
    int verbose = 0;
    int no_length_correction = 0;
    int compact_results = 0;

    double tolerance = 0.0001;

    std::string extension = ".pcl.bz2";
    std::string gene_file;
    std::string input_filename;
    std::string transcript_length_file;
    std::string output_filename_isoform_tpm;
    std::string output_filename_isoform_counts;
    std::string output_filename_gene_tpm;
    std::string output_filename_gene_counts;
    std::string samples_str;

    bool bad_args = false;

    static struct option long_options[] = {
        {"help", no_argument, 0, 'h'},
        {"model", required_argument, 0, 'm'},
        {"compact-results", no_argument, &compact_results, 'c'},
        {"no-length-correction", no_argument, &no_length_correction, 'n'},
        {"outbase", required_argument, 0, 'o'},
        {"transcript-lengths", required_argument, 0, 'l'},
        {"max-iterations", required_argument, 0, 'i'},
        {"gene-mappings", required_argument, 0, 'g'},
        {"samples", required_argument, 0, 's'},
        {"verbose", no_argument, &verbose, 1},
        {"version", no_argument, 0, 'V'},
        {"tolerance", required_argument, 0, 't'},
        {0, 0, 0, 0}
    };

    int c;
    int option_index = 0;

    while ((c = getopt_long(argc, argv, "hm:no:i:l:g:s:vVct:", long_options,
                            &option_index)) != -1) {
        switch (c) {
            case 'c':
                compact_results = 1;
                break;

            case 'g':
                gene_file = std::string(optarg);
                break;

            case 'h':
                print_help();
                return 0;

            case 'i':
                max_iterations = std::stoi(optarg);
                break;

            case 'l':
                transcript_length_file = std::string(optarg);
                break;

            case 'm':
                m = std::stoi(optarg);
                if (m < 1 || m > 4) {
                    std::cerr << "[ERROR] Invalid model number specified. Valid options: 1, 2, 3, 4\n";
                }

                switch (m) {
                    case 1:
                        model = SampleAllelicExpression::MODEL_1;
                        break;
                    case 2:
                        model = SampleAllelicExpression::MODEL_2;
                        break;
                    case 3:
                        model = SampleAllelicExpression::MODEL_3;
                        break;
                    case 4:
                        model = SampleAllelicExpression::MODEL_4;
                        break;
                }

                break;


            case 'n':
                no_length_correction = 1;
                break;

            case 'o':
                output_filename_isoform_tpm = std::string(optarg);
                output_filename_isoform_counts = std::string(optarg);
                output_filename_gene_tpm = std::string(optarg);
                output_filename_gene_counts = std::string(optarg);

                output_filename_isoform_tpm.append(".isoform.tpm");
                output_filename_isoform_counts.append(".isoform.counts");
                output_filename_gene_tpm.append(".gene.tpm");
                output_filename_gene_counts.append(".gene.counts");
                break;

            case 's':
                all_samples = 0;
                samples_str = std::string(optarg);
                break;

            case 't':
                tolerance = std::stod(optarg);
                break;

            case 'v':
                verbose = 1;
                break;

            case 'V':
                std::cout << VERSION << std::endl;
                return 0;

            case '?':
                bad_args = true;
        }
    }

    if (bad_args) {
        print_help();
        return 1;
    }

    if (argc - optind == 1) {
        input_filename = argv[optind];
    } else {
        std::cerr << "\n[ERROR] Missing required argument (input file name)" << std::endl;
        print_help();
        return 1;
    }

    if (output_filename_isoform_tpm.empty()) {
        //use default, based on the input file name but placed in current working directdory
        output_filename_isoform_tpm = "emase-zero.quantified.isoform.tpm";
        output_filename_isoform_counts = "emase-zero.quantified.isoform.counts";
        output_filename_gene_tpm = "emase-zero.quantified.gene.tpm";
        output_filename_gene_counts = "emase-zero.quantified.gene.counts";

    }

    std::cout << "\nemase-zero Version " << VERSION << std::endl <<std::endl;
    std::cout << "Alignment File: " << input_filename << std::endl;

    if (!transcript_length_file.empty()) {
        std::cout << "Transcript Length File: " << transcript_length_file
                  << std::endl;

        if (no_length_correction) {
            std::cerr << "\n[ERROR] -l cannot be used with -n" << std::endl;
            print_help();
            return 1;
        }
    }
    else {
        std::cout << "Transcript Length File: None" << std::endl;
    }

    if (no_length_correction) {
        std::cout << "Length Correction: No " << std::endl;

        if (!transcript_length_file.empty()) {
            std::cerr << "\n[ERROR] -n cannot be used with -l" << std::endl;
            print_help();
            return 1;
        }
    } else {
        std::cout << "Length Correction: Yes " << std::endl;
    }

    std::cout << compact_results << std::endl;

    if (compact_results) {
        std::cout << "Compact Results: Yes" << std::endl;
    } else {
        std::cout << "Compact Results: No" << std::endl;
    }

    if (!gene_file.empty()) {
        std::cout << "Grouping File: " << gene_file << std::endl;
    } else {
        std::cout << "Grouping File: None" << std::endl;
    }

    int gzipped = isGZipped(input_filename);
    int format = getBinFormat(input_filename);

    if (gzipped) {
        std::cout << "Compressed: Yes" << std::endl;
    } else {
        std::cout << "Compressed: No" << std::endl;
    }

    std::cout << "Format: " << format << std::endl;

    if (samples_str.length() > 0) {
        if (format != 2) {
            std::cerr << "\n[ERROR] Samples are not supported in format 0 or 1" << std::endl;
            print_help();
            return 1;
        }

        std::size_t location = samples_str.find(':', 0);

        if (location != std::string::npos) {
            std::string s_start = samples_str.substr(0, location);
            std::string s_end = samples_str.substr(location + 1);

            sample_start = std::stoi(s_start);
            sample_end = std::stoi(s_end);
        } else {
            sample_start = std::stoi(samples_str);
            sample_end = sample_start + 1;
        }
        
        std::cout << "Sample start: " << sample_start << std::endl;
        std::cout << "Sample end: " << sample_end << std::endl;
    }

    std::cout << "Loading " << input_filename << "..." << std::endl;
    aim = loadFromBin(input_filename);

    if (!aim) {
        std::cerr << "[Error] Error in binary input file" << std::endl;
        return 1;
    }

    if (all_samples) {
        sample_start = 0;
        sample_end = aim->num_samples();
    }

    // bounds checking for samples

    if (sample_start >= sample_end) {
        std::cerr << "[Error] end sample should be greater than start sample" << std::endl;
        return 1;
    }

    if ((aim->num_samples() == 1) && ((sample_start > 0) || (sample_end > 1))) {
        std::cerr << "[ERROR] Only 1 sample detected, do not specify sample with -s" << std::endl;
        return 1;
    }

    if ((sample_start > aim->num_samples()) || (sample_end > aim->num_samples())) {
        std::cerr << "[ERROR] Samples requested must be between 0 and " << aim->num_samples() - 1 << std::endl;
        return 1;
    }

    std::vector<std::string> hap_names = aim->get_haplotype_names();
    std::vector<std::string> sample_names = aim->get_sample_names();

    if (verbose) {
        std::cout << "File had the following haplotype names:" << std::endl;
        for (auto it = hap_names.begin(); it != hap_names.end(); ++it) {
            std::cout << *it << std::endl;
        }

        if (format != 2) {
            if (aim->has_equivalence_classes()) {
                std::cout << aim->num_alignment_classes()
                          << " alignment classes loaded ("
                          << aim->total_reads() << " total reads)"
                          << std::endl;
            }
        }
        std::cout << aim->num_transcripts() << " transcripts" << std::endl;

    }

    if (!transcript_length_file.empty()) {
        if (verbose) {
            std::cout << "Loading Transcript Length File "
                      << transcript_length_file << std::endl;
        }
        aim->loadTranscriptLengths(transcript_length_file);
    }

    if (no_length_correction) {
        if (verbose) {
            std::cout << "No length correction, setting all lengths to 1.0" << std::endl;
        }
        aim->setTranscriptLengths(1.0);
    }

    if (!gene_file.empty()) {
        if (verbose) {
            std::cout << "Loading Gene Mapping File " << gene_file << std::endl;
        }
        aim->loadGeneMappings(gene_file);
        if (verbose) {
            std::cout << "Gene Mappings Loaded" << std::endl;
        }
    }

    if (model != SampleAllelicExpression::MODEL_4 && !aim->has_gene_mappings()) {
        std::cerr << "[ERROR] File does not contain transcript to gene mapping information.  Only normalization Model 4 can be used.\n";
        return 1;
    }

    bool output_header = true;

    std::cout << "EM Model: " << model << std::endl
              << "Output File (Isoform TPM): " << output_filename_isoform_tpm << std::endl
              << "Output File (Isoform Counts): " << output_filename_isoform_counts << std::endl;


    if(aim->has_gene_mappings()) {
        std::cout << "Output File (Gene TPM): " << output_filename_gene_tpm << std::endl
                  << "Output File (Gene Counts): " << output_filename_gene_counts << std::endl;

    }

    std::cout << "----------------------------------------------------\n\n\n";


    // Loop through all the samples specified
    for (int i = sample_start; i < sample_end; ++i) {
        if (format == 2) {
            std::cout << "SAMPLE " << i << " : " << sample_names[i] << std::endl;
            loadNFromBin(input_filename, *aim, i);
        }

        if (verbose) {
            std::cout << aim->total_reads() << " reads loaded" << std::endl;
        }

        t1 = clock();
        SampleAllelicExpression sae(aim, tolerance);
        t2 = clock();

        if (verbose) {
            diff = ((float)t2-(float)t1)/CLOCKS_PER_SEC;
            std::cout << "Time for initializing stack sum = " << diff << "s"
                      << std::endl;
        }

        if (max_iterations > 0) {
            num_iterations = 0;
            std::cout << "Running EM..." << std::endl;
            double change;
            double iteration_time;
            bool  converged;
            clock_t t1_inner, t2_inner;

            if (verbose) {
                std::cout << std::setw(7) << "Iter No" << "    "
                        << std::setw(8) << "Time(s)" << "    "
                        << std::setw(16) << "Change" << std::endl
                        << "-------    --------    ----------------\n";
            }

            t1 = clock();

            do {
                t1_inner = clock();
                sae.update(model);
                t2_inner = clock();
                converged = sae.converged(change, verbose);

                if (verbose) {
                    std::cout << std::setw(7) << num_iterations + 1 << "    "
                            << std::setw(8) << std::setprecision(3)
                            << ((float)t2_inner - (float)t1_inner)/CLOCKS_PER_SEC
                            << "    " << std::setw(16) << std::setprecision(1)
                            << std::fixed << change << std::endl;
                }
            } while (++num_iterations < max_iterations && !converged);
            
            t2 = clock();

            diff = ((float)t2 - (float)t1) / CLOCKS_PER_SEC;
            std::cout << "Time for " << num_iterations << " iterations = " << diff << "s" << std::endl;
            std::cout << "Time per iteration " << std::setprecision(2) << diff/num_iterations << "s" << std::endl;
        }

        std::cout << "Saving results..." << std::endl;

        // remove the file at the start, append each sample
        if (i == sample_start) {
            remove(output_filename_isoform_tpm.c_str());
            remove(output_filename_isoform_counts.c_str());

            if(aim->has_gene_mappings()) {
                remove(output_filename_gene_tpm.c_str());
                remove(output_filename_gene_counts.c_str());
            }
        }

        if (format == 2) {
            sae.updateNoApplyTL(model);
            sae.saveStackSums(output_filename_isoform_counts, output_filename_gene_counts, sample_names[i], output_header, compact_results);
            sae.applyTranscriptLength(true);
            sae.saveStackSums(output_filename_isoform_tpm, output_filename_gene_tpm, sample_names[i], output_header, compact_results);
            output_header = false;
            std::cout << "Done." << std::endl;
        } else {
            sae.updateNoApplyTL(model);
            sae.saveStackSums(output_filename_isoform_counts, output_filename_gene_counts, compact_results);
            sae.applyTranscriptLength(true);
            sae.saveStackSums(output_filename_isoform_tpm, output_filename_gene_tpm, compact_results);
        }

    }

    std::cout << "Program finished." << std::endl;

    return 0;
}
bool test(bool is_kernel_exact = true)
{
	// types
  typedef typename K::FT FT;
  typedef typename K::Line_3 Line;
  typedef typename K::Point_3 Point;
  typedef typename K::Segment_3 Segment;
  typedef typename K::Ray_3 Ray;
  typedef typename K::Line_3 Line;
  typedef typename K::Triangle_3 Triangle;

  /* -------------------------------------
  // Test data is something like that (in t supporting plane)
  // Triangle is (p1,p2,p3)
  //
  //       +E          +1
  //                 /   \
  //        +C     6+  +8  +4      +B
  //              /   9++7  \
  //            3+-------+5--+2
  //     
  //         +F        +A      
  ------------------------------------- */
  
  Point p1(FT(1.), FT(0.), FT(0.));
  Point p2(FT(0.), FT(1.), FT(0.));
  Point p3(FT(0.), FT(0.), FT(1.));
  
  Triangle t(p1,p2,p3);
  
  // Edges of t 
  Segment s12(p1,p2);
  Segment s21(p2,p1);
  Segment s13(p1,p3);
  Segment s23(p2,p3);
  Segment s32(p3,p2);
  Segment s31(p3,p1);
  
  bool b = test_aux(is_kernel_exact,t,s12,"t-s12",s12);
  b &= test_aux(is_kernel_exact,t,s21,"t-s21",s21);
  b &= test_aux(is_kernel_exact,t,s13,"t-s13",s13);
  b &= test_aux(is_kernel_exact,t,s23,"t-s23",s23);

  // Inside points
  Point p4(FT(0.5), FT(0.5), FT(0.));
  Point p5(FT(0.), FT(0.75), FT(0.25));
  Point p6(FT(0.5), FT(0.), FT(0.5));
  Point p7(FT(0.25), FT(0.625), FT(0.125));
  Point p8(FT(0.5), FT(0.25), FT(0.25));
  
  Segment s14(p1,p4);
  Segment s41(p4,p1);
  Segment s24(p2,p4);
  Segment s42(p4,p2);
  Segment s15(p1,p5);
  Segment s25(p2,p5);
  Segment s34(p3,p4);
  Segment s35(p3,p5);
  Segment s36(p3,p6);
  Segment s45(p4,p5);
  Segment s16(p1,p6);
  Segment s26(p2,p6);
  Segment s62(p6,p2);
  Segment s46(p4,p6);
  Segment s48(p4,p8);
  Segment s56(p5,p6);
  Segment s65(p6,p5);
  Segment s64(p6,p4);
  Segment s17(p1,p7);
  Segment s67(p6,p7);
  Segment s68(p6,p8);
  Segment s86(p8,p6);
  Segment s78(p7,p8);
  Segment s87(p8,p7);
  
  b &= test_aux(is_kernel_exact,t,s14,"t-s14",s14);
  b &= test_aux(is_kernel_exact,t,s41,"t-s41",s41);
  b &= test_aux(is_kernel_exact,t,s24,"t-s24",s24);
  b &= test_aux(is_kernel_exact,t,s42,"t-s42",s42);
  b &= test_aux(is_kernel_exact,t,s15,"t-s15",s15);
  b &= test_aux(is_kernel_exact,t,s25,"t-s25",s25);
  b &= test_aux(is_kernel_exact,t,s34,"t-s34",s34);
  b &= test_aux(is_kernel_exact,t,s35,"t-s35",s35);
  b &= test_aux(is_kernel_exact,t,s36,"t-s36",s36);
  b &= test_aux(is_kernel_exact,t,s45,"t-s45",s45);
  b &= test_aux(is_kernel_exact,t,s16,"t-s16",s16);
  b &= test_aux(is_kernel_exact,t,s26,"t-s26",s26);
  b &= test_aux(is_kernel_exact,t,s62,"t-s62",s62);
  b &= test_aux(is_kernel_exact,t,s46,"t-s46",s46);
  b &= test_aux(is_kernel_exact,t,s65,"t-s65",s65);
  b &= test_aux(is_kernel_exact,t,s64,"t-s64",s64);
  b &= test_aux(is_kernel_exact,t,s48,"t-s48",s48);
  b &= test_aux(is_kernel_exact,t,s56,"t-s56",s56);
  b &= test_aux(is_kernel_exact,t,s17,"t-t17",s17);
  b &= test_aux(is_kernel_exact,t,s67,"t-t67",s67);
  b &= test_aux(is_kernel_exact,t,s68,"t-s68",s68);
  b &= test_aux(is_kernel_exact,t,s86,"t-s86",s86);
  b &= test_aux(is_kernel_exact,t,s78,"t-t78",s78);
  b &= test_aux(is_kernel_exact,t,s87,"t-t87",s87);
  
  // Outside points (in triangle plane)
  Point pA(FT(-0.5), FT(1.), FT(0.5));
  Point pB(FT(0.5), FT(1.), FT(-0.5));
  Point pC(FT(0.5), FT(-0.5), FT(1.));
  Point pE(FT(1.), FT(-1.), FT(1.));
  Point pF(FT(-1.), FT(0.), FT(2.));
  
  Segment sAB(pA,pB);
  Segment sBC(pB,pC);
  Segment s2E(p2,pE);
  Segment sE2(pE,p2);
  Segment s2A(p2,pA);
  Segment s6E(p6,pE);
  Segment sB8(pB,p8);
  Segment sC8(pC,p8);
  Segment s8C(p8,pC);
  Segment s1F(p1,pF);
  Segment sF6(pF,p6);
  
  b &= test_aux(is_kernel_exact,t,sAB,"t-sAB",p2);
  b &= test_aux(is_kernel_exact,t,sBC,"t-sBC",s46);
  b &= test_aux(is_kernel_exact,t,s2E,"t-s2E",s26);
  b &= test_aux(is_kernel_exact,t,sE2,"t-sE2",s62);
  b &= test_aux(is_kernel_exact,t,s2A,"t-s2A",p2);
  b &= test_aux(is_kernel_exact,t,s6E,"t-s6E",p6);
  b &= test_aux(is_kernel_exact,t,sB8,"t-sB8",s48);
  b &= test_aux(is_kernel_exact,t,sC8,"t-sC8",s68);
  b &= test_aux(is_kernel_exact,t,s8C,"t-s8C",s86);
  b &= test_aux(is_kernel_exact,t,s1F,"t-s1F",s13);
  b &= test_aux(is_kernel_exact,t,sF6,"t-sF6",s36);
  
  // Outside triangle plane
  Point pa(FT(0.), FT(0.), FT(0.));
  Point pb(FT(2.), FT(0.), FT(0.));
  Point pc(FT(1.), FT(0.), FT(1.));
  Point pe(FT(1.), FT(0.5), FT(0.5));
  
  Segment sab(pa,pb);
  Segment sac(pa,pc);
  Segment sae(pa,pe);
  Segment sa8(pa,p8);
  Segment sb2(pb,p2);
  
  b &= test_aux(is_kernel_exact,t,sab,"t-sab",p1);
  b &= test_aux(is_kernel_exact,t,sac,"t-sac",p6);
  b &= test_aux(is_kernel_exact,t,sae,"t-sae",p8);
  b &= test_aux(is_kernel_exact,t,sa8,"t-sa8",p8);
  b &= test_aux(is_kernel_exact,t,sb2,"t-sb2",p2);
  
  // -----------------------------------
  // ray queries
  // -----------------------------------
  // Edges of t 
  Ray r12(p1,p2);
  Ray r21(p2,p1);
  Ray r13(p1,p3);
  Ray r23(p2,p3);
  
  b &= test_aux(is_kernel_exact,t,r12,"t-r12",s12);
  b &= test_aux(is_kernel_exact,t,r21,"t-r21",s21);
  b &= test_aux(is_kernel_exact,t,r13,"t-r13",s13);
  b &= test_aux(is_kernel_exact,t,r23,"t-r23",s23);
  
  // In triangle
  Point p9_(FT(0.), FT(0.5), FT(0.5));
  Point p9(FT(0.25), FT(0.375), FT(0.375));
  
  Ray r14(p1,p4);
  Ray r41(p4,p1);
  Ray r24(p2,p4);
  Ray r42(p4,p2);
  Ray r15(p1,p5);
  Ray r25(p2,p5);
  Ray r34(p3,p4);
  Ray r35(p3,p5);
  Ray r36(p3,p6);
  Ray r45(p4,p5);
  Ray r16(p1,p6);
  Ray r26(p2,p6);
  Ray r62(p6,p2);
  Ray r46(p4,p6);
  Ray r48(p4,p8);
  Ray r56(p5,p6);
  Ray r47(p4,p7);
  Ray r89(p8,p9);
  Ray r86(p8,p6);
  Ray r68(p6,p8);
  Segment r89_res(p8,p9_);
  
  b &= test_aux(is_kernel_exact,t,r14,"t-r14",s12);
  b &= test_aux(is_kernel_exact,t,r41,"t-r41",s41);
  b &= test_aux(is_kernel_exact,t,r24,"t-r24",s21);
  b &= test_aux(is_kernel_exact,t,r42,"t-r42",s42);
  b &= test_aux(is_kernel_exact,t,r15,"t-r15",s15);
  b &= test_aux(is_kernel_exact,t,r25,"t-r25",s23);
  b &= test_aux(is_kernel_exact,t,r34,"t-r34",s34);
  b &= test_aux(is_kernel_exact,t,r35,"t-r35",s32);
  b &= test_aux(is_kernel_exact,t,r36,"t-r36",s31);
  b &= test_aux(is_kernel_exact,t,r45,"t-r45",s45);
  b &= test_aux(is_kernel_exact,t,r16,"t-r16",s13);
  b &= test_aux(is_kernel_exact,t,r26,"t-r26",s26);
  b &= test_aux(is_kernel_exact,t,r62,"t-r62",s62);
  b &= test_aux(is_kernel_exact,t,r46,"t-r46",s46);
  b &= test_aux(is_kernel_exact,t,r48,"t-r48",s46);
  b &= test_aux(is_kernel_exact,t,r56,"t-r56",s56);
  b &= test_aux(is_kernel_exact,t,r47,"t-r47",s45);
  b &= test_aux(is_kernel_exact,t,r89,"t-t89",r89_res);
  b &= test_aux(is_kernel_exact,t,r68,"t-r68",s64);
  b &= test_aux(is_kernel_exact,t,r86,"t-r86",s86);
  
  
  // Outside points (in triangre prane)
  Ray rAB(pA,pB);
  Ray rBC(pB,pC);
  Ray r2E(p2,pE);
  Ray rE2(pE,p2);
  Ray r2A(p2,pA);
  Ray r6E(p6,pE);
  Ray rB8(pB,p8);
  Ray rC8(pC,p8);
  Ray r8C(p8,pC);
  Ray r1F(p1,pF);
  Ray rF6(pF,p6);
  
  b &= test_aux(is_kernel_exact,t,rAB,"t-rAB",p2);
  b &= test_aux(is_kernel_exact,t,rBC,"t-rBC",s46);
  b &= test_aux(is_kernel_exact,t,r2E,"t-r2E",s26);
  b &= test_aux(is_kernel_exact,t,rE2,"t-rE2",s62);
  b &= test_aux(is_kernel_exact,t,r2A,"t-r2A",p2);
  b &= test_aux(is_kernel_exact,t,r6E,"t-r6E",p6);
  b &= test_aux(is_kernel_exact,t,rB8,"t-rB8",s46);
  b &= test_aux(is_kernel_exact,t,rC8,"t-rC8",s64);
  b &= test_aux(is_kernel_exact,t,r8C,"t-r8C",s86);
  b &= test_aux(is_kernel_exact,t,r1F,"t-r1F",s13);
  b &= test_aux(is_kernel_exact,t,rF6,"t-rF6",s31);
  
  // Outside triangle plane
  Ray rab(pa,pb);
  Ray rac(pa,pc);
  Ray rae(pa,pe);
  Ray ra8(pa,p8);
  Ray rb2(pb,p2);
  
  b &= test_aux(is_kernel_exact,t,rab,"t-rab",p1);
  b &= test_aux(is_kernel_exact,t,rac,"t-rac",p6);
  b &= test_aux(is_kernel_exact,t,rae,"t-rae",p8);
  b &= test_aux(is_kernel_exact,t,ra8,"t-ra8",p8);
  b &= test_aux(is_kernel_exact,t,rb2,"t-rb2",p2);
  
  // -----------------------------------
  // Line queries
  // -----------------------------------
  // Edges of t 
  Line l12(p1,p2);
  Line l21(p2,p1);
  Line l13(p1,p3);
  Line l23(p2,p3);
  
  b &= test_aux(is_kernel_exact,t,l12,"t-l12",s12);
  b &= test_aux(is_kernel_exact,t,l21,"t-l21",s21);
  b &= test_aux(is_kernel_exact,t,l13,"t-l13",s13);
  b &= test_aux(is_kernel_exact,t,l23,"t-l23",s23);
  
  // In triangle
  Line l14(p1,p4);
  Line l41(p4,p1);
  Line l24(p2,p4);
  Line l42(p4,p2);
  Line l15(p1,p5);
  Line l25(p2,p5);
  Line l34(p3,p4);
  Line l35(p3,p5);
  Line l36(p3,p6);
  Line l45(p4,p5);
  Line l16(p1,p6);
  Line l26(p2,p6);
  Line l62(p6,p2);
  Line l46(p4,p6);
  Line l48(p4,p8);
  Line l56(p5,p6);
  Line l47(p4,p7);
  Line l89(p8,p9);
  Line l86(p8,p6);
  Line l68(p6,p8);
  Segment l89_res(p1,p9_);

  
  b &= test_aux(is_kernel_exact,t,l14,"t-l14",s12);
  b &= test_aux(is_kernel_exact,t,l41,"t-l41",s21);
  b &= test_aux(is_kernel_exact,t,l24,"t-l24",s21);
  b &= test_aux(is_kernel_exact,t,l42,"t-l42",s12);
  b &= test_aux(is_kernel_exact,t,l15,"t-l15",s15);
  b &= test_aux(is_kernel_exact,t,l25,"t-l25",s23);
  b &= test_aux(is_kernel_exact,t,l34,"t-l34",s34);
  b &= test_aux(is_kernel_exact,t,l35,"t-l35",s32);
  b &= test_aux(is_kernel_exact,t,l36,"t-l36",s31);
  b &= test_aux(is_kernel_exact,t,l45,"t-l45",s45);
  b &= test_aux(is_kernel_exact,t,l16,"t-l16",s13);
  b &= test_aux(is_kernel_exact,t,l26,"t-l26",s26);
  b &= test_aux(is_kernel_exact,t,l62,"t-l62",s62);
  b &= test_aux(is_kernel_exact,t,l46,"t-l46",s46);
  b &= test_aux(is_kernel_exact,t,l48,"t-l48",s46);
  b &= test_aux(is_kernel_exact,t,l56,"t-l56",s56);
  b &= test_aux(is_kernel_exact,t,l47,"t-l47",s45);
  b &= test_aux(is_kernel_exact,t,l89,"t-t89",l89_res);
  b &= test_aux(is_kernel_exact,t,l68,"t-l68",s64);
  b &= test_aux(is_kernel_exact,t,l86,"t-l86",s46);

  
  // Outside points (in triangle plane)
  Line lAB(pA,pB);
  Line lBC(pB,pC);
  Line l2E(p2,pE);
  Line lE2(pE,p2);
  Line l2A(p2,pA);
  Line l6E(p6,pE);
  Line lB8(pB,p8);
  Line lC8(pC,p8);
  Line l8C(p8,pC);
  Line l1F(p1,pF);
  Line lF6(pF,p6);
  
  b &= test_aux(is_kernel_exact,t,lAB,"t-lAB",p2);
  b &= test_aux(is_kernel_exact,t,lBC,"t-lBC",s46);
  b &= test_aux(is_kernel_exact,t,l2E,"t-l2E",s26);
  b &= test_aux(is_kernel_exact,t,lE2,"t-lE2",s62);
  b &= test_aux(is_kernel_exact,t,l2A,"t-l2A",p2);
  b &= test_aux(is_kernel_exact,t,l6E,"t-l6E",s26);
  b &= test_aux(is_kernel_exact,t,lB8,"t-lB8",s46);
  b &= test_aux(is_kernel_exact,t,lC8,"t-lC8",s64);
  b &= test_aux(is_kernel_exact,t,l8C,"t-l8C",s46);
  b &= test_aux(is_kernel_exact,t,l1F,"t-l1F",s13);
  b &= test_aux(is_kernel_exact,t,lF6,"t-lF6",s31);
  
  // Outside triangle plane
  Line lab(pa,pb);
  Line lac(pa,pc);
  Line lae(pa,pe);
  Line la8(pa,p8);
  Line lb2(pb,p2);
  
  b &= test_aux(is_kernel_exact,t,lab,"t-lab",p1);
  b &= test_aux(is_kernel_exact,t,lac,"t-lac",p6);
  b &= test_aux(is_kernel_exact,t,lae,"t-lae",p8);
  b &= test_aux(is_kernel_exact,t,la8,"t-la8",p8);
  b &= test_aux(is_kernel_exact,t,lb2,"t-lb2",p2);
  
  
	return b;
}
void BlueCoreDeviceController_newStyle::Implementation::checkQueues ()
{
    PDU pdu(null_pdu);
    for ( int i = 0 ; i < PDU::csr_hci_extensions_count ; ++i )
    {
        for ( int j = 0 ; j < 8 && pending[i]->getNextIfAvailable(pdu) ; ++j )
        {
            mConnectionToChip->sendpdu ( pdu );
            if (pdu.channel() == PDU::hciACL)
                aclDataTracker.removeData(pdu);
        }
    }
    // deal with packets coming up...
    for ( int j = 0 ; j < 8 && upstream.get( pdu ) ; ++j )
    {
        PDU::bc_channel ch = pdu.channel();

        switch ( ch )
        {
        case PDU::bccmd:
            pending[PDU::bccmd]->setToken();
            break;
        case PDU::hq :
        {
            if ( HQ_PDU(pdu).get_req_type() == HQPDU_SETREQ )
            {
                HQ_PDU ret ( pdu.clone () );
                ret.set_req_type ( HQPDU_GETRESP );
                pending[PDU::hq]->add(sae(ret,no_call_back));
            }
            break;
        }
        case PDU::hciCommand:
        {
            HCIEventPDU ev ( pdu );
            switch ( ev.get_event_code() )
            {
            case HCI_EV_NUMBER_COMPLETED_PKTS:
            {
                HCI_EV_NUMBER_COMPLETED_PKTS_T_PDU ncp( pdu );
                uint8 count = ncp.get_num_handles();
                for ( uint8 i = 0 ; i < count ; ++i )
                {
                    uint16 handle;
                    uint16 tokens;
                    ncp.get_num_completed_pkts(i,handle,tokens);
                    pending[PDU::hciACL]->incToken(tokens);
                }
            }
                break;
            case HCI_EV_COMMAND_COMPLETE:
            {
                HCICommandCompletePDU cc (pdu);
                pending[PDU::hciCommand]->setToken(cc.get_num_hci_command_pkts() );
                switch(cc.get_op_code())
                {
                case HCI_READ_VOICE_SETTING:
                {
                    HCI_READ_VOICE_SETTING_RET_T_PDU rvs(pdu);
                    if ( rvs.get_status() == HCI_SUCCESS )
                    {
                        bool is_it_16 = (rvs.get_voice_setting()
                                       & HCI_VOICE_SAMP_SIZE_MASK)
                                      == HCI_VOICE_SAMP_SIZE_16BIT;
                        scoBandwidthTracker.set_audio_sample_size ( is_it_16 ? 16 : 8 );
                    }
                }
                    break;
                case HCI_READ_BUFFER_SIZE:
                    if ( !initialised_ACL_flow_control )
                    {
                        HCI_READ_BUFFER_SIZE_RET_T_PDU rbs ( pdu );
                        if ( rbs.get_status() == HCI_SUCCESS )
                        {
                            initialised_ACL_flow_control = true;
                            pending[PDU::hciACL]->setToken ( rbs.get_total_acl_data_pkts() );
                        }
                    }
                    break;
                }
            }
                break;
            case HCI_EV_COMMAND_STATUS:
            {
                HCI_EV_COMMAND_STATUS_T_PDU cc (pdu);
                pending[PDU::hciCommand]->setToken(cc.get_num_hci_command_pkts() );
                if (cc.get_op_code() == HCI_ADD_SCO_CONNECTION
                 && cc.get_status() != HCI_COMMAND_CURRENTLY_PENDING )
                {
                    getTransport()->set_sco_bandwidth(scoBandwidthTracker.decrease());
                }
            }
                break;
            case HCI_EV_CONN_COMPLETE:
            {
                HCI_EV_CONN_COMPLETE_T_PDU cc(pdu);
                if (cc.get_status() == HCI_SUCCESS)
                {
                    if (cc.get_link_type() == HCI_LINK_TYPE_ACL)
                        aclDataTracker.addConnection(cc.get_handle());
                    else
                    {
                        // sco or esco.  This will glitch if we were the initiating end.
                        getTransport()->set_sco_bandwidth(scoBandwidthTracker.consolidate(cc.get_handle()));
                    }
                }
                else
                {
                    if (cc.get_link_type() == HCI_LINK_TYPE_SCO)
                    {
                        getTransport()->set_sco_bandwidth(scoBandwidthTracker.decrease());
                    }
                }
            }
                break;
            case HCI_EV_DISCONNECT_COMPLETE:
            {
                HCI_EV_DISCONNECT_COMPLETE_T_PDU dc(pdu);
                if ( dc.get_status() == HCI_SUCCESS )
                {
                    uint16 h = dc.get_handle();
                    if (aclDataTracker.usingConnection(h))
                        aclDataTracker.removeConnection(h);
                    else
                    {
                        // sco or esco
                        getTransport()->set_sco_bandwidth(scoBandwidthTracker.decrease(h));
                    }
                }
            }
                break;
            case HCI_EV_SYNC_CONN_COMPLETE:
            {
                HCI_EV_SYNC_CONN_COMPLETE_T_PDU scc(pdu);
                if ( scc.get_status() == HCI_SUCCESS )
                {
                    // sco or esco.  This will glitch if we were the initiating end.
                    getTransport()->set_sco_bandwidth(scoBandwidthTracker.consolidate(scc.get_handle()));
                }
                else
                    scoBandwidthTracker.decrease();
            }
                break;
            case HCI_EV_LOOPBACK_COMMAND:
                // we just had a command loop back, so allow us to send another.
                pending[PDU::hciCommand]->setToken(1);
                break;
            default:
                break;
            }
            break;
        }
        default:
            break;
        }

        if ( !pending[ch]->run_callback(pdu) )
            mReceiver.onPDU ( pdu );
        //  some stuff should be passed up... HQ and HCI only?
        if ( mConnectionOnOffer )
        {
            switch ( ch )
            {
            case PDU::hq:
            case PDU::hciCommand:
            case PDU::hciACL:
            case PDU::hciSCO:
                (void)sendUp ( pdu );
                break;
            default:
                // do nothing
                break;
            }
        }
    }
    //  accept requests from higher layers on all channels.
    for ( int k = 0 ; k < 8 && downstream.get( pdu ) ; ++k )
    {
        switch ( pdu.channel() )
        {
        case PDU::hciCommand:
        case PDU::hciACL:
        case PDU::hciSCO:
            (void)send ( sae ( pdu , perm_no_pass ) , true );
            break;
        default:
            (void)send ( sae ( pdu , perm_pass_up ) , true );
            break;
        }
    }
}
bool BlueCoreDeviceController_newStyle::Implementation::sendRawAny ( PDU::bc_channel channel , const uint8 *aBuffer , uint32 aLength , bool withFlowControl )
{
    return send ( sae ( PDU ( channel , aBuffer , aLength ) , no_call_back ) , withFlowControl );
}